计算时间差【C++】

计算时间差【C++】

时间: 1ms        内存:128M

描述:

对于按照"yyyymmddhhmmss"格式给定的时间,计算两个时间的差值。其中yyyy 表示年(0000~3000),mm表示月(00~12),dd表示日(00~31),hh表示时(00~23),mm表示分(00~59),ss表示秒(00~59)。已给定类部分代码,不可删除和修改已有代码,只可以扩充。

输入:

第一行整数n(n<=100),表示需要计算的时间差值数目。以后n行,每行两个用空格隔开的按照"yyyymmddhhmmss"格式的数字串。

输出:

每组时间差值输出单独一行。用空格分隔的两个数,表示两个时间相差的天数和秒数。

示例输入:

2
20130527193000 20130527180000
20131231235959 20130101000000

示例输出:

0 5400
364 86399

提示:

参考答案(内存最优[1268]):

#include <iostream>
#include <string>
using namespace std;

class DateTime {

public:

    int year;

    int month;

    int day;

    int hour;

    int minute;

    int seconds;

public:

    void TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds);

    //对t1 ,t2 表示的两个时间计算时间差值,将相差的天数保存在days中,将相差的秒数保存在seconds中

};

void DateTime::TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds)
{

    int i;
    int days1,days2;
    int seconds1,seconds2;
    days1=0,days2=0;
    seconds1=0,seconds2=0;

    for(i=2000;i<t1.year;i++)
    {
        if((i%4==0&&i%100!=0)||i%400==0)
            days1+=366;
        else
            days1+=365;
    }
    for(i=1;i<t1.month;i++)
    {
        switch(i)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:days1+=31;break;
        case 4:
        case 6:
        case 9:
        case 11:days1+=30;break;
        case 2:
            {
                if((t1.year%4==0&&t1.year%100!=0)||t1.year%400==0)
                    days1+=29;
                else
                    days1+=28;
            };break;
        }
    }
    days1+=t1.day;




    for(i=2000;i<t2.year;i++)
    {
        if((i%4==0&&i%100!=0)||i%400==0)
            days2+=366;
        else
            days2+=365;
    }
    for(i=1;i<t2.month;i++)
    {
        switch(i)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:days2+=31;break;
        case 4:
        case 6:
        case 9:
        case 11:days2+=30;break;
        case 2:
            {
                if((t2.year%4==0&&t2.year%100!=0)||t2.year%400==0)
                    days2+=29;
                else
                    days2+=28;
            };break;
        }
    }
    days2+=t2.day;

    days=days2-days1;
    if(days<0)
        days=0-days;


    for(i=0;i<t1.hour;i++)
        seconds1+=(60*60);
    for(i=0;i<t1.minute;i++)
        seconds1+=60;
    seconds1+=t1.seconds;

    for(i=0;i<t2.hour;i++)
        seconds2+=(60*60);
    for(i=0;i<t2.minute;i++)
        seconds2+=60;
    seconds2+=t2.seconds;

    seconds=seconds2-seconds1;
    if(seconds<0)
        seconds=0-seconds;
}

int main()
{

    DateTime a1,a2;
 //   string c,d;
	char c[15],d[15];
    int n;
    int dayss,secondss;
    cin>>n;
    for(;n>0;n--)
    {
        cin>>c;
        cin>>d;
        a1.year=(c[0]-'0')*1000+(c[1]-'0')*100+(c[2]-'0')*10+c[3]-'0';
		a1.month=(c[4]-'0')*10+(c[5]-'0');
		a1.day=(c[6]-'0')*10+(c[7]-'0');
		a1.hour=(c[8]-'0')*10+(c[9]-'0');
		a1.minute=(c[10]-'0')*10+(c[11]-'0');
		a1.seconds=(c[12]-'0')*10+(c[13]-'0');

		a2.year=(d[0]-'0')*1000+(d[1]-'0')*100+(d[2]-'0')*10+(d[3]-'0');
		a2.month=(d[4]-'0')*10+(d[5]-'0');
		a2.day=(d[6]-'0')*10+(d[7]-'0');
		a2.hour=(d[8]-'0')*10+(d[9]-'0');
		a2.minute=(d[10]-'0')*10+(d[11]-'0');
		a2.seconds=(d[12]-'0')*10+(d[13]-'0');

 //       cout<<a1.year<<" "<<a1.month<<" "<<a1.day<<" "<<a1.hour<<" "<<a1.minute<<" "<<a1.seconds<<endl;
//		cout<<a2.year<<" "<<a2.month<<" "<<a2.day<<" "<<a2.hour<<" "<<a2.minute<<" "<<a2.seconds<<endl;
		a1.TimeDiff(a1,a2,dayss,secondss);
        cout<<dayss<<" "<<secondss<<endl;
    }
    return 0;
}

参考答案(时间最优[0]):

#include<stdio.h>
#include <iostream>
using namespace std;

class DateTime {

public:

    int year;

    int month;

    int day;

    int hour;

    int minute;

    int seconds;

public:

    void TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds);

    //对t1 ,t2 表示的两个时间计算时间差值,将相差的天数保存在days中,将相差的秒数保存在seconds中
    void display(void);
};
void DateTime::display(void)
{
    cout<<year<<' '<<month<<' '<<day<<' '<<hour<<' '<<minute<<' '<<seconds<<endl;
}
void DateTime::TimeDiff(DateTime& t1,DateTime& t2,int& days,int& seconds)
{
    bool run(DateTime);
    int run_day(DateTime&);
    int norun_day(DateTime&);
    if(t1.hour==t2.hour && t1.minute==t2.minute){
        if(t1.seconds>=t2.seconds){
            seconds=t1.seconds-t2.seconds;
        }
        else{
            seconds=t2.seconds-t1.seconds;
        }
    }
    else if(t1.hour==t2.hour){
        seconds=0;
        int s1,s2;
        if(t1.minute>=t2.minute){
            s1=t1.seconds;
            s2=60-t2.seconds;
            seconds=(t1.minute-t2.minute-1)*60+s1+s2;
        }
        else{
            s2=t2.seconds;
            s1=60-t1.seconds;
            seconds=(t2.minute-t1.minute-1)*60+s1+s2;
        }
    }
    else{
        seconds=0;
        int s1,s2;
        if(t1.hour>t2.hour){
            s1=t1.minute*60+t1.seconds;
            s2=3600-t2.minute*60-t2.seconds;
            seconds=(t1.hour-t2.hour-1)*3600+s1+s2;
        }
        else{
            s2=t2.minute*60+t2.seconds;
            s1=3600-t1.minute*60-t1.seconds;
            seconds=(t2.hour-t1.hour-1)*3600+s1+s2;
        }
    }


    if(t1.year==t2.year && t1.month==t2.month){
        if(t1.day>=t2.day)
            days=t1.day-t2.day;
        else
            days=t2.day-t1.day;
    }
    else if(t1.year==t2.year){
        if(run(t1)){  //如果是闰年
            int d1,d2;
            d1=run_day(t1);
            d2=run_day(t2);
            if(d1>=d2)
                days=d1-d2;
            else
                days=d2-d1;
        }
        else{   //不是闰年
            int d1,d2;
            d1=norun_day(t1);
            d2=norun_day(t2);
            if(d1>=d2)
                days=d1-d2;
            else
                days=d2-d1;
        }
    }
    else{   //年月日都不相同的情况
        DateTime y1,y2;
        if(t1.year>=t2.year){
            y1=t1;
            y2=t2;
        }
        else{
            y1=t2;
            t2=t1;
        }
        //保持y1>y2
        int sum=0;
        if(run(y1))
            sum+=run_day(t1);
        else
            sum+=norun_day(t1);
        y1.year-=1;
        while(y1.year!=y2.year){
            if(run(y1)){
                sum+=366;
            }
            else
                sum+=365;
            y1.year--;
        }
        if(run(y1)){
            sum=sum+(366-run_day(y2));
        }
        else{
            sum=sum+(365-norun_day(y2));
        }
    }
}
bool run(DateTime t1)
{
    bool f;
        if(t1.year%100==0){
            if(t1.year%400==0)
                f=true;
            else
                f=false;
        }
        else{
            if(t1.year%4==0)
                f=true;
            else
                f=false;
        }
        return f;
}

int run_day(DateTime& t)
{
    int d;
    switch(t.month){
            case 1:d=t.day;break;
            case 2:d=31+t.day;break;
            case 3:d=60+t.day;break;
            case 4:d=91+t.day;break;
            case 5:d=121+t.day;break;
            case 6:d=152+t.day;break;
            case 7:d=182+t.day;break;
            case 8:d=213+t.day;break;
            case 9:d=244+t.day;break;
            case 10:d=274+t.day;break;
            case 11:d=305+t.day;break;
            case 12:d=335+t.day;break;
            }
    return d;
}
int norun_day(DateTime& t)
{
    int d;
    switch(t.month){
            case 1:d=t.day;break;
            case 2:d=31+t.day;break;
            case 3:d=59+t.day;break;
            case 4:d=90+t.day;break;
            case 5:d=120+t.day;break;
            case 6:d=151+t.day;break;
            case 7:d=181+t.day;break;
            case 8:d=212+t.day;break;
            case 9:d=243+t.day;break;
            case 10:d=273+t.day;break;
            case 11:d=304+t.day;break;
            case 12:d=334+t.day;break;
            }
    return d;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        DateTime t1,t2;
        scanf("%4d%2d%2d%2d%2d%2d",&t1.year,&t1.month,&t1.day,&t1.hour,&t1.minute,&t1.seconds);
        scanf("%4d%2d%2d%2d%2d%2d",&t2.year,&t2.month,&t2.day,&t2.hour,&t2.minute,&t2.seconds);
        DateTime t3;
        int days,seconds;
        t3.TimeDiff(t1,t2,days,seconds);
        cout<<days<<' '<<seconds<<endl;
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注