题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <iostream> using namespace std; bool isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } return false; } int main() { int f[2][13]={ {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; int x,y,m,d,n; scanf("%d",&x);//一共x组数据 for(int j=0;j<x;j++){ scanf("%d %d %d %d",&y,&m,&d,&n);//年,月,日,加的日子 n=n+d; d=1; while(n>f[isLeapYear(y)][m]){ n-=f[isLeapYear(y)][m]; if(m==12){ m=1;y++; } else{ m++; } } d=n; printf("%d-%02d-%02d\n",y,m,d); } } // 64 位输出请用 printf("%lld")