题解 | #日期差值#

日期差值

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

思路

把日期全部归到当年的开始 不用考虑两个月份的大小关系 注意边界问题

#AC代码

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int M[13][2]={
    {31,31},
    {28,29},
    {31,31},
    {30,30},
    {31,31},
    {30,30},
    {31,31},
    {31,31},
    {30,30},
    {31,31},
    {30,30},
    {31,31}
};

int is_leap(int y){
    if((y%4==0 && y%100 != 0) || y%400==0) return 1;
    return 0;
}

int main(){
    int s1,s2;
    int y1,m1,d1,y2,m2,d2;
    cin >> s1 >> s2;
    if(s1>s2)  swap(s1,s2);
    y1=s1/10000,m1=s1%10000/100,d1=s1%100;
    y2=s2/10000,m2=s2%10000/100,d2=s2%100;
    int s=0;
    int year1=is_leap(y1);
    for(int i=0;i<m1-1;i++){
        s -= M[i][year1];
    }
    s -= d1;
    for(int i=y1;i<y2;i++){
        if(is_leap(i))  s+=366;
        else s+=365;
    }
    int year2=is_leap(y2);
    for(int i=0;i<m2-1;i++){
        s += M[i][year2];
    }
    s+=d2;
    cout << s+1 << endl;
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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