题解 | #日期累加#日期累加思路简单解法
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <iostream> using namespace std; bool IsLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } int main() { int m; cin >> m; int year, month, day, daysum; while ((m--) && (cin >> year >> month >> day >> daysum)) { // 注意 while 处理多个 case //先给出每个月的日期数组 int monthday[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //将当前的日期的day和要加的日期加在一起 daysum += day; while (daysum > monthday[month]) { //若是闰年就是29否则28 if (IsLeapYear(year)) { monthday[2] = 29; } else { monthday[2] = 28; } //减点当前月的日期,然后month++ daysum -= monthday[month]; month++; //如果month加到了12以上则将year++,将month重置为1 if (month == 13) { year++; month = 1; } } printf("%04d-%02d-%02d\n", year, month, daysum); } } // 64 位输出请用 printf("%lld")#日期转换问题##日期累加#