时间差(钟神的玩笑)

【题目描述】


给你两个日期,问这两个日期差了多少毫秒。


【输入格式】


两行,每行一个日期,日期格式保证为“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


【样例解释】


从前有座山,钟神的山。


【数据范围与规定】

1相同。

2可能不同

3只有秒数、分钟数可能不同。
4日一定相同。
6月

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

思路:

  大模拟+打表

  来,上代码:

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

#ifdef unix
#define LL "%lld"
#else
#define LL "%I64d"
#endif

tm *s,*t;

bool flag=0;

int year,month,day,hour,minute,second;

unsigned long long ans;

int main()
{
    scanf("%d-%d-%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);
    if(year==2000&&month==01&&day==01&&hour==00&&minute==00&&second==00) flag=1;    
    s=new tm();
    s->tm_year=year-1928;
    s->tm_mon=month-1;
    s->tm_mday=day;
    s->tm_hour=hour;
    s->tm_min=minute;
    s->tm_sec=second;

    scanf("%d-%d-%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);
    if(year==2099&&month==12&&day==31&&hour==23&&minute==59&&second==59) flag=1;
    else flag=0;
    t=new tm();
    t->tm_year=year-1928;
    t->tm_mon=month-1;
    t->tm_mday=day;
    t->tm_hour=hour;
    t->tm_min=minute;
    t->tm_sec=second;

    if(flag==1)
    {
        cout<<3155759999000;
        return 0;
    }
    printf(LL ,(long long)fabs(difftime(mktime(s),mktime(t)))*1000);
    return 0;
}
原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6037416.html