第二题

第二题

【题目描述】

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

【输入格式】

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

【样例解释】

从前有座山。

【数据范围与规定】

对于的数据,两个日期相同。

对于的数据,两个日期只有秒数可能不同。

对于的数据,两个日期只有秒数、分钟数可能不同。

对于的数据,两个日期的年月日一定相同。

对于的数据,两个日期的年月一定相同。

对于的数据,两个日期的年份一定相同。

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

 

 

思路:这个题的代码用ctime来实现很方便,但是只能得40分;AC代码是传统的累加计算,关键点在于判断平年和闰年(能被4整除的是闰年,不能被4整除的是平年,能被100整除但不能被400整除的是平年)

 1 //40分代码(用ctime)
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<ctime>
 6 #include<cmath>
 7 
 8 using namespace std;
 9 
10 #ifdef unix
11 #define LL "%lld"
12 #else
13 #define LL "%I64d"
14 #endif
15 
16 tm *s,*t;
17 
18 int year,month,day,hour,minute,second;
19 
20 int main()
21 {
22     freopen("two.in","r",stdin);
23     freopen("two.out","w",stdout);
24 
25     scanf("%d-%d-%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);
26     s=new tm();
27     s->tm_year=year-1900;s->tm_mon=month-1;s->tm_mday=day;s->tm_hour=hour;s->tm_min=minute;s->tm_sec=second;
28 
29     scanf("%d-%d-%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);
30     t=new tm();
31     t->tm_year=year-1900;t->tm_mon=month-1;t->tm_mday=day;t->tm_hour=hour;t->tm_min=minute;t->tm_sec=second;
32 
33     printf(LL "
",(long long)fabs(difftime(mktime(s),mktime(t)))*1000);
34 
35 
36     return 0;
37 }
 1 //AC代码
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #define LL long long
 6 using namespace std;
 7 LL n1,y1,r1,s1,f1,m1;
 8 LL n2,y2,r2,s2,f2,m2;
 9 LL ans;
10 LL month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
11 LL init()
12 {
13     LL x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 LL judge(LL x)
19 {
20     if((x%4==0&&x%100!=0)||x%400==0)
21       return 1;
22     return 0;
23 }
24 void Deal()
25 {
26     if(m1==60)m1=0,f1++;
27     if(f1==60)f1=0,s1++;
28     if(s1==24)s1=0,r1++;
29     if(judge(n1))month[2]=29;
30     else month[2]=28;
31     if(r1==month[y1]+1)r1=1,y1++;
32     if(y1==13)y1=1,n1++;
33 }
34 int main()
35 {
36     freopen("two.in","r",stdin);
37    freopen("two.out","w",stdout);
38     n1=init(),y1=init(),r1=init(),s1=init(),f1=init(),m1=init();
39     n2=init(),y2=init(),r2=init(),s2=init(),f2=init(),m2=init();
40     if(m1!=m2)
41     {
42         if(m1<m2)ans+=m2-m1,m1=m2;
43         else if(m1>m2)ans+=60-m1+m2,m1=m2,f1++;
44         Deal();
45     }
46     if(f1!=f2)
47     {
48         if(f1<f2)ans+=(f2-f1)*60,f1=f2;
49         else if(f1>f2)ans+=(60-f1+f2)*60,f1=f2,s1++;
50         Deal();
51     }
52     if(s1!=s2)
53     {
54         if(s1<s2)ans+=(s2-s1)*60*60,s1=s2;
55         else if(s1>s2)ans+=(24-s1+s2)*60*60,s1=s2,r1++;
56         Deal();
57     }
58     while(r1!=r2||y1!=y2||n1!=n2)
59     {
60         r1++;Deal();
61         ans+=60*60*24;
62     }
63     cout<<ans*1000<<endl;
64     return 0;
65 }

 

原文地址:https://www.cnblogs.com/thmyl/p/7359415.html