zoukankan      html  css  js  c++  java
  • 时间计算

    【题目描述】 给你两个日期,问这两个日期差了多少毫秒。

    【输入格式】 两行,每行一个日期,日期格式保证为“YYYY-MM-DD hh:mm:ss”这种形 式。第二个日期时间一定比第一个日期时间要大两个日期的年份一定都是 21 世 纪的年份。

    【输出格式】 一行一个整数代表毫秒数。

    【样例输入 1】 2000-01-01 00:00:00 2000-01-01 00:00:01 【样例输出 1】 1000

    【样例输入 2】 2000-01-01 00:00:00 2000-11-11 00:00:00 【样例输出 2】 27216000000

     【数据范围与规定】 对于10%的数据,两个日期相同。 对于20%的数据,两个日期只有秒数可能不同。

    对于30%的数据,两个日期只有秒数、分钟数可能不同。 对于40%的数据,两个日期的年月日一定相同。 对于60%的数据,两个日期的年月一定相同。

    对于80%的数据,两个日期的年份一定相同。 P73 第二题 第 3 页

    对于100%的数据,两个日期一定都是 21 世纪的某一天,且第二个日期一定 大于等于第一个日期。

    思路:

      模拟吧,好麻烦,搞错一个变量,幸好检查出来了,但只改了一处其他地方没改。。。

      

      第一遍90  第二遍100

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    long long  yeara,moutha,daya,aa,ba,ca;
    long long  yearb,mouthb,dayb,ab,bb,cb;
    long long ans;
    int main()
    {
        freopen("two.in","r",stdin);
        freopen("two.out","w",stdout);
        char c1,c2,c3,c4,c5,c6;
        scanf("%lld%c%lld%c%lld%c%lld%c%lld%c%lld",&yearb,&c1,&mouthb,&c2,&dayb,&c3,&ab,&c4,&bb,&c5,&cb);
        scanf("%lld%c%lld%c%lld%c%lld%c%lld%c%lld",&yeara,&c1,&moutha,&c2,&daya,&c3,&aa,&c4,&ba,&c5,&ca);
        
        if(yeara==yearb)
        {
            for(long long i=mouthb+1;i<=moutha-1;i++)
            {
                if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                    ans+=1LL*31*24*60*60;
                else
                if(i==4||i==6||i==9||i==11)
                    ans+=1LL*30*24*60*60;
                else 
                if(i==2)
                    if((yearb%4) ==0)
                        ans+=29LL*24*60*60;
                    else ans+=28LL*24*60*60;
            }
            if(moutha>mouthb)
            {
                ans+=(daya)*24LL*60*60;
                if(mouthb==1||mouthb==3||mouthb==5||mouthb==7||mouthb==8||mouthb==10||mouthb==12)
                    ans+=(31-dayb)*1LL*24*60*60;
                else
                if(mouthb==4||mouthb==6||mouthb==9||mouthb==11)
                    ans+=(30-dayb)*1LL*24*60*60;
                else 
                if(mouthb==2)
                    if((yeara%4) ==0)
                        ans+=(29-dayb)*1LL*24*60*60;
                    else ans+=(28-dayb)*1LL*24*60*60;
            }else         
                ans+=(daya-dayb)*24LL*60*60;
            
            ans+=(aa-ab)*60*60LL;
            ans+=(ba-bb)*60LL;
            ans+=(ca-cb);
            cout<<ans*1000;
            return 0;
        }else
        {
            for(int i=yearb+1;i<=yeara-1;i++)
                if(i%4)    ans+=365LL*24*60*60;
                else ans+= 366LL*24*60*60; 
            
            for(long long i=1;i<=moutha-1;i++)        
            {
                if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                    ans+=1LL*31*24*60*60;
                else
                if(i==4||i==6||i==9||i==11)
                    ans+=1LL*30*24*60*60;
                else 
                if(i==2)
                    if((yeara%4) ==0)
                        ans+=29LL*24*60*60;
                    else ans+=28LL*24*60*60;
            }
            for(int i=mouthb+1;i<=12;i++)
            {
                if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                    ans+=1LL*31*24*60*60;
                else
                if(i==4||i==6||i==9||i==11)
                    ans+=1LL*30*24*60*60;
                else 
                if(i==2)
                    if((yearb%4) ==0)
                        ans+=29LL*24*60*60;
                    else ans+=28LL*24*60*60;
            }        
            if(mouthb==1||mouthb==3||mouthb==5||mouthb==7||mouthb==8||mouthb==10||mouthb==12)
                    ans+=(31-dayb)*1LL*24*60*60;
            else
            if(mouthb==4||mouthb==6||mouthb==9||mouthb==11)
                ans+=(30-dayb)*1LL*24*60*60;
            else 
            if(mouthb==2)
                if((yearb%4) ==0)
                    ans+=(29-dayb)*1LL*24*60*60;
                else ans+=(28-dayb)*1LL*24*60*60;
                
            ans+=(daya)*24LL*60*60;
            ans+=(aa-ab)*60*60LL;//上边改了,这里没改
            ans+=(ba-bb)*60LL;
            ans+=(ca-cb);
            cout<<ans*1000;
        //    cout<<ans/3600/24;
            return 0;
        }
        return  0;
    }
  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7630535.html
Copyright © 2011-2022 走看看