题解 | #日期差值#

日期差值

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

#include <iostream>
using namespace std;
int M[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isrun(int y){
    if(y%400==0||(y%4==0&&y%100!=0)) return true;
    return false;
}
void add(int& y,int& m,int& d){
    if(isrun(y)) M[2]=29;else M[2]=28;
    d++;if(d>M[m]) {d=1;m++;if(m>12){m=1;y++;}}
    
}
int main() {
    string a, b;int ans=0;
   cin >> a >> b;if(a>b) swap(a,b);
        int y1=stoi(a.substr(0,4));
        int m1=stoi(a.substr(4,2)); int d1=stoi(a.substr(6,2));
          int y2=stoi(b.substr(0,4));
        int m2=stoi(b.substr(4,2)); int d2=stoi(b.substr(6,2));
        while(y1!=y2||m1!=m2||d1!=d2){
            add(y1,m1,d1);ans++;
        }
        
        cout<<ans+1;
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务