是闰年的情况:
能够被400整除的年份是闰年;
能够被4整除且不能被100整除的年份是闰年;
if ((year%400==0) || ((year%100!=0)&&(year%4==0)))
{
//是闰年
cout<<"is leap year"<<endl;
}
else
{
//不是闰年
cout<<"not leap year!"<<endl;
}
完整代码:
#include<iostream>
using namespace std;
int main()
{
int year;
int num;
bool continue_loop = true; //控制循环是否继续检查年份
while(continue_loop)
{
cout<<"请输入年份: "<<endl;
cin>>year;
if ((year%400==0) || ((year%100!=0)&&(year%4==0)))
{
//是闰年
cout<<"is leap year!\n"<<endl;
}
else
{
//不是闰年
cout<<"not leap year!\n"<<endl;
}
cout<<"需要继续判断?(输入“1”继续|输入其他退出)"<<endl;
cin>>num;
cout<<endl;
if(num!=1)
{
cout<<"\n已退出!"<<endl;
continue_loop = false;
}
}
return 0;
}
本文介绍了一种用于判断闰年的算法,详细解释了闰年的定义及其判断条件。文章提供了完整的C++代码实现,包括一个循环机制,允许用户多次输入年份进行判断,并选择是否继续操作。
1377

被折叠的 条评论
为什么被折叠?



