《算法笔记》3.4小节——入门模拟->日期处理
D无难度,注意格式是yyyy-mm-dd,年也是要补足的,样例里面没有提醒但是自己要注意啊!!
#include<stdio.h> int month[13][2]={0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31}; bool isrun(int year) { if((year%4==0 && year%100!=0) || year%400==0) return true; return false; } int main() { int y,n; while(scanf("%d %d",&y,&n)!=EOF) { int i=1; while(n-month[i][isrun(y)]>0) { n=n-month[i][isrun(y)]; i++; if(i==13) { i=1; y++; } } printf("%04d-%02d-%02d ",y,i,n); } return 0; }
E没啥难度,注意跟前面的A区分,这里面两天的差值,相隔一天是一天,A题中相邻两天是两天
这里面从去年到今年的同一天(今年当天不算),对应闰年是365,对应平年364
#include<stdio.h> int month[13][2]={0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31}; bool isrun(int year) { if((year%4==0 && year%100!=0) || year%400==0) return true; return false; } int main() { int n; scanf("%d",&n); while(n--) { int y,m,d,sum; scanf("%d %d %d %d",&y,&m,&d,&sum); int data=m*100+d; while(sum>=365) { if(data<=229) { if(isrun(y)) { sum=sum-365; //y++; if(data==229) { data=301; m=3; d=1; } } else sum=sum-364; y++; } else { if(isrun(y+1)) { sum=sum-365; } else sum=sum-364; y++; } } while(sum>0) { d++; sum--; if(d>month[m][isrun(y)]) { d=1; m++; if(m>12) { m=1; y++; } } } printf("%04d-%02d-%02d ",y,m,d); } return 0; }