CCF认证真题-(201509-2)-日期计算

 

思路:

先判断是不是闰年, 如果是, 使存月份天数的数组month二月的位置+1(即从28天变29天).

 1 #include <iostream>
 2 using namespace std;
 3 int month[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 4 
 5 int main()
 6 {
 7     ios::sync_with_stdio(false);
 8     cin.tie(0);
 9 
10     int y, d;
11     cin >> y >> d;
12 
13     // 是否为闰年
14     if ((y % 4 == 0 && y % 100)|| (y % 400 == 0)) month[2]++;
15 
16     // 月份和日期
17     int m = 1, date;
18     while (d > month[m]) {
19         d -= month[m];
20         m++;
21     }
22     date = d;
23     cout << m << endl << date << endl;
24 
25     return 0;
26 }

 

posted @ 2019-07-10 17:45  域Anton  阅读(173)  评论(0)    收藏  举报