当前位置: 代码迷 >> 综合 >> 【codeup 1928】日期差值(P91)
  详细解决方案

【codeup 1928】日期差值(P91)

热度:88   发布时间:2023-12-05 07:44:01.0
#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}};//因为数组下标是从0开始,月份是1~12,就是为了让序号与月份对应。 
bool isLeap(int year)//判断是否为闰年
{return (year%4==0&&year%100!=0)||(year%400==0);
} 
int main()
{int time1,y1,m1,d1;int time2,y2,m2,d2;while(scanf("%d%d",&time1,&time2)!=EOF){if(time1>time2){int t=time1;time1=time2;time2=t;}y1=time1/10000,m1=time1%10000/100,d1=time1%100;y2=time2/10000,m2=time2%10000/100,d2=time2%100;int days=1;//记录结果 //第一个日期没有到达第二个日期时,循环 //!((y1==y2)&&(m1==m2)&&(d1==d2))while((y1<y2)||(m1<m2)||(d1<d2)){d1++;if(d1==month[m1][isLeap(y1)]+1){m1++;d1=1;}if(m1==13)//如果月份为13,则年数加一,月份变为1 {y1++;m1=1;}days++;} printf("%d\n",days);}return 0;
}