codeup - 入门模拟->日期处理 - 人间大迷惑

17 篇文章 0 订阅
10 篇文章 0 订阅

ac代码

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<time.h>

using namespace std;
/*

20130101
20130105
20130205
20130101
20000101
20010105
20130101
20140105
*/

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}
    };
int year[2]={365,366};

int isLeap(int year) {
    return ((year%400==0)||(year%4==0 && year%100!=0));
}
int main()
{
    int date1,date2;
    int y1,m1,d1,tmp,y2,m2,d2;
    while(cin>>date1>>date2) {
        if((date1-date2)>0){
            tmp=date1;
            date1=date2;
            date2=tmp;
        }
        y1=date1/10000;
        tmp=date1-y1*10000;
        m1=tmp/100;
        d1=(tmp-m1*100)%100;
//        cout<<y1<<endl<<m1<<endl<<d1<<endl;

        y2=date2/10000;
        tmp=date2-y2*10000;
        m2=tmp/100;
        d2=(tmp-m2*100)%100;

        int ans=1;

        while((y1<y2)||(m1<m2)||(d1<d2)) {
            d1++;
            if(d1==month[m1][isLeap(y1)]+1) {
                m1++;
                d1=1;
            }
            if(m1==13) {
                y1++;
                m1=1;
            }
            ans++;

        }
        cout<<ans<<endl;
    }

}

测试数据

1. day
20130101
20130105
ans: 5

2. month
20130205
20130101
ans: 36

3. year(isleap)
20000101
20130105
ans: 371

4. year(isNotleap)
20130101
20140105
ans: 370

数据如下:

20130101
20130105
20130205
20130101
20000101
20010105
20130101
20140105

刚开始一直运行超限0(运行超时),但是查了下时间复杂度,也和别人的ac代码进行了对比,感觉问题并不是出在while循环中。

但是修改了一下判断闰年的函数int isLeap(int year)(我以为只有能整除400才是闰年,经典缺乏常识了),就能过了。

真是人间大迷惑= =|||

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值