题解 | #日期差值#

日期差值

https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c

#include <iostream>
using namespace std;

int months[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 isRun(int year){
    if(year%400==0 || (year%4==0 && year%100!=0)) return 1;
    else return 0;
}

int main() {
   int y1,m1,d1,y2,m2,d2,sum,minn;
   int day1,day2;
   char chr1[8],chr2[8];
   while(scanf("%04d%02d%02d\n%04d%02d%02d",&y1,&m1,&d1,&y2,&m2,&d2)!=EOF){
      sum=0;
      minn=0;
      day1=0,day2=0;
      
      minn=min(y1,y2);
      //计算day1
      for(int i=minn;i<y1;i++){
            day1+=(365+isRun(i));
      }
      for(int i=1;i<m1;i++){
          day1+=months[isRun(y1)][i];
      }
      day1+=d1;

      //计算day2
      for(int i=minn;i<y2;i++){
            day2+=(365+isRun(i));
      }
      for(int i=1;i<m2;i++){
          day2+=months[isRun(y1)][i];
      }
      day2+=d2;

      cout<<abs(day2-day1)+1<<endl;
   }
   return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务