题目描述:
解题思路:
代码实现:
#include <iostream>
using namespace std;
bool IsLeapYear(int x) {
return ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0));
}
int month[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
struct Date {
int year;
int month;
int day;
};
int CountDay(Date* date) {
int cnt = 0;
for (int i = 1; i < date->month; i++) {
cnt += month[IsLeapYear(date->year)][i];
}
cnt += date->day;
return cnt;
}
int main() {
cout << "please input date(year month day):" << endl;
Date date;
cin >> date.year >> date.month >> date.day;
int ans = CountDay(&date);
cout << "the " << ans << "'th day in the year" << endl;
return 0;
}