解题思路:利用类进行处理;
注意事项:平年、闰年的判断。
参考代码:
#include<iostream> class Time { private:int year, month, day; public: Time(int y, int m, int d); int daysofyear(); }; int daysofmonth(int year,int month); Time::Time(int y, int m,int d) { year = y; month = m; day = d; } int daysofmonth(int year,int month) { int a[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { a[1] = 29; } return a[month - 1]; } int Time::daysofyear() { int i; int sum = 0; for (i = 1; i < month; i++) { sum += daysofmonth(year,i); } sum += day; return sum; } int main(int argc, char** argv) { int y, m, d; std::cin >> y >> m >> d; Time t1(y,m,d); std::cout << t1.daysofyear(); return 0; }
0.0分
0 人评分