设计一个程序能计算一个日期加上若干天后是什么日期。
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
52008 2 29 10002008 2 29 312008 2 29 322007 2 28 312017 2 17 1主要难点在于对于闰年判断后如何对增加日期的处理问题
#include<stdio.h>#include<math.h>#include<iostream>using namespace std;bool judge(int year){ if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return true; } return false;}int main(){// freopen("E:/input.txt", "r", stdin); int m; cin >> m; int a[][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; while (m--) { int year, month, day, num; cin >> year >> month >> day >> num; int b = 0, i = 0; if (judge(year)) { b = 1; } if (num <= a[b][month - 1] - day) { day += num; } else { num -= (a[b][month - 1] - day); if (month == 12) { month = 0; i = 0; year++; if(judge(year)) { b = 1; } else { b = 0; } } month++; day = 0; for (i = month - 1; ; i++) { if (num > a[b][i]) { num -= a[b][i]; if (month == 12) { month = 0; i = -1; //因为for循环会增1 year++; if(judge(year)) { b = 1; } else { b = 0; } } month++; } else { day += num; break; } } } cout << year << "-"; if (month < 10) { cout <<"0"; } cout << month << "-"; if (day < 10) { cout << "0"; } cout << day << endl;; } return 0;}新闻热点
疑难解答