题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&tqId=31013&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking
通过暴力计算,遍历每个月的天数即可。(tip:day在开始时设为0,天数加到plus上,便于处理。)
#include<bits/stdc++.h> using namespace std; int monthDay[2][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}};// 闰年 int isRunYear(int year) { return (year%4==0&&year%100!=0) || (year%400==0); } int main() { int year, month, day, plusDay; int n; while(scanf("%d", &n) != EOF) { for(int i=0; i<n; i++) { // 1992 11 22 316 scanf("%d%d%d%d", &year, &month, &day, &plusDay); int runOrPing = isRunYear(year); plusDay += day; day = 0; while(plusDay>monthDay[runOrPing][month-1]) { plusDay-=monthDay[runOrPing][month-1]; if(month == 12) { year++; month = 1; runOrPing = isRunYear(year); } else { month++; } } day = plusDay; printf("%d-%02d-%02d\n", year, month, day); } } return 0; }