是闰年的情况:
能够被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;
}