設計一個程序能計算一個日期加上若干天后是什么日期。
輸入第一行表示樣例個數(shù)m,接下來m行每行四個整數(shù)分別表示年月日和累加的天數(shù)。
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循環(huán)會增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;}新聞熱點
疑難解答