给你当期日期,问前D天和后D天的年月日。
思路:
直接模拟就行了水题,对了别忘了题目2013,3,5要输出这样的2013/03/05.
#include<stdio.h> typedef struct { int n ,y ,r; }Time; int PY[13] = {0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31}; int RY[13] = {0 ,31 ,29 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31}; bool jude(int y) { return y % 4 == 0 && y % 100 || y % 400 == 0; } Time add(int D) { int nown = 2013 ,nowy = 3 ,nowr = 24; while(D--) { nowr ++; if(jude(nown) && nowr > RY[nowy] || !jude(nown) && nowr > PY[nowy]) { nowy ++; nowr = 1; } if(nowy > 12) { nown ++; nowy = 1; } } Time ans; ans.n = nown ,ans.y = nowy ,ans.r = nowr; return ans; } Time sub(int D) { int nown = 2013 ,nowy = 3 ,nowr = 24; while(D--) { nowr --; if(!nowr) { nowy --; if(!nowy){nowy = 12 ; nown --;} if(jude(nown)) nowr = RY[nowy]; else nowr = PY[nowy]; } } Time ans; ans.n = nown ,ans.y = nowy ,ans.r = nowr; return ans; } int main () { int t ,D ,i ,j ,k; scanf("%d" ,&t); while(t--) { scanf("%d" ,&D); Time A = add(D); Time B = sub(D); printf("%04d/%02d/%02d %04d/%02d/%02d " ,A.n ,A.y ,A.r ,B.n ,B.y ,B.r); } return 0; }