我们C++的课本上有个题目,是这样写的:“输出21世纪所有的闰年。判断闰年的条件是:能被4整除但是不能被100整除,或者能被400整除。”我试了好几种方法写,但是写出来的还是错的,有谁能帮我写个正确的参考一下,非常的感谢。
21世纪所有的闰年就是2000-2999,闰年的计算方法:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;被100整除也可被400整除的为闰年。如2000年是闰年,而1900年不是。 你的计算逻辑不对。
if((i % 100 == 0 && i % 400 == 0) || i % 4 == 0){
//输出i的值
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2020-05-25
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"二十一世纪中"<<endl;
for (a=2000;a<3000;a++)
if (a%400==0)
cout<<a<<"为闰年"<<endl;
else if (a%4==0&&a%100!=0)
cout<<a<<"为闰年"<<endl;
return 0;
}
using namespace std;
int main()
{
int a;
cout<<"二十一世纪中"<<endl;
for (a=2000;a<3000;a++)
if (a%400==0)
cout<<a<<"为闰年"<<endl;
else if (a%4==0&&a%100!=0)
cout<<a<<"为闰年"<<endl;
return 0;
}
第2个回答 2017-07-20
bool isLeapYear(int year)
{
return (year % 400 == 0) ? true : ((year % 100 == 0) ? false : ((year % 4 == 0) ? true : false));
}
{
return (year % 400 == 0) ? true : ((year % 100 == 0) ? false : ((year % 4 == 0) ? true : false));
}
第3个回答 2013-05-25
这个这样写就好了,哪有那么麻烦啊!!!#include <stdio.h>
int main(void)
{
int year;
year=2000;
for(year=2000;year<=2100;year++){
if((year%4==0&&year%100!=0)||(year%400==0)){
printf("%d\n",year);
}
}
return 0;
}本回答被网友采纳
int main(void)
{
int year;
year=2000;
for(year=2000;year<=2100;year++){
if((year%4==0&&year%100!=0)||(year%400==0)){
printf("%d\n",year);
}
}
return 0;
}本回答被网友采纳
第4个回答 推荐于2017-06-15
#include <iostream> using namespace std; class Date{
public:
Date(int d1 = 1900, int d2 = 1, int d3 = 1): year(d1), month(d2), day(d3) { }
void setYear(int y) { year = y; }
void setMonth(int m) { month = m; }
void setDay(int d) { day = d; }
void Date::setDate(int, int, int);
int getDay() { return day; }
int getMonth() { return month; }
int getYear() { return year; }
void print();
bool isLeapYear();
int monthDays();
void nextDay();
private:
int year;
int month;
int day;
};bool Date::isLeapYear()
{
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
{
return true;
}else
{
return false;
}
}
int Date::monthDays()
{
int sum = 0;
switch(month)/*先计算某月以前月份的总天数*/
{
case 1: sum = 31; break;
case 2: sum = 28; break;
case 3: sum = 31; break;
case 4: sum = 30; break;
case 5: sum = 31; break;
case 6: sum = 30; break;
case 7: sum = 31; break;
case 8: sum = 31; break;
case 9: sum = 30; break;
case 10:sum = 31; break;
case 11:sum = 30; break;
case 12:sum = 31; break;
default:cout << "data error";break;
}
if(isLeapYear() && month == 2)/*如果是闰年且月份等于于2,总天数应该加一天*/
sum++;
return sum;
}
void Date::setDate(int yr, int mo, int dy)
{
if(yr < 1900)
cerr << "year error" << endl;
if(mo > 12)
cerr << "month error" << endl;
if(monthDays() < day)
cerr << "day error" << endl;
year = yr;
month = mo;
day = dy;
}
void Date::print()
{
cout << "year: " << year << " month: " << month << " day: " << day << endl;
cout << " is Leap Year : " << (isLeapYear()?"true":"false") << endl;
cout << " monthDays : " << monthDays() << endl;
}
void Date::nextDay()
{
if(monthDays() == day) // 月的最后一天
{
if(month == 12) // 月是最后一个月(跨年)
{
++year;
month = 1;
day = 1;
}else
{
++month;
day = 1;
}
}else
{
++day;
}
}
int main()
{
int y, m, d;
Date da;
cin >> y >> m >> d;
da.setDate(y,m,d);
da.print();
//da.nextDay(); // 下一天的函数调用
//da.print();
system("pause");
return 0;
}本回答被网友采纳
public:
Date(int d1 = 1900, int d2 = 1, int d3 = 1): year(d1), month(d2), day(d3) { }
void setYear(int y) { year = y; }
void setMonth(int m) { month = m; }
void setDay(int d) { day = d; }
void Date::setDate(int, int, int);
int getDay() { return day; }
int getMonth() { return month; }
int getYear() { return year; }
void print();
bool isLeapYear();
int monthDays();
void nextDay();
private:
int year;
int month;
int day;
};bool Date::isLeapYear()
{
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
{
return true;
}else
{
return false;
}
}
int Date::monthDays()
{
int sum = 0;
switch(month)/*先计算某月以前月份的总天数*/
{
case 1: sum = 31; break;
case 2: sum = 28; break;
case 3: sum = 31; break;
case 4: sum = 30; break;
case 5: sum = 31; break;
case 6: sum = 30; break;
case 7: sum = 31; break;
case 8: sum = 31; break;
case 9: sum = 30; break;
case 10:sum = 31; break;
case 11:sum = 30; break;
case 12:sum = 31; break;
default:cout << "data error";break;
}
if(isLeapYear() && month == 2)/*如果是闰年且月份等于于2,总天数应该加一天*/
sum++;
return sum;
}
void Date::setDate(int yr, int mo, int dy)
{
if(yr < 1900)
cerr << "year error" << endl;
if(mo > 12)
cerr << "month error" << endl;
if(monthDays() < day)
cerr << "day error" << endl;
year = yr;
month = mo;
day = dy;
}
void Date::print()
{
cout << "year: " << year << " month: " << month << " day: " << day << endl;
cout << " is Leap Year : " << (isLeapYear()?"true":"false") << endl;
cout << " monthDays : " << monthDays() << endl;
}
void Date::nextDay()
{
if(monthDays() == day) // 月的最后一天
{
if(month == 12) // 月是最后一个月(跨年)
{
++year;
month = 1;
day = 1;
}else
{
++month;
day = 1;
}
}else
{
++day;
}
}
int main()
{
int y, m, d;
Date da;
cin >> y >> m >> d;
da.setDate(y,m,d);
da.print();
//da.nextDay(); // 下一天的函数调用
//da.print();
system("pause");
return 0;
}本回答被网友采纳