标签:
一开始调用一个函数,结果竟然超时了,后来将闰年判断换成数组存储,就过了。可能每一次都来判断一次就比较耗时,一次都判断完就省时间了。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2615 | Accepted: 811 |
Description
Input
Output
Sample Input
7 0:0:0 1.1.2000 10:10:10 1.3.2001 0:12:13 1.3.2400 23:59:59 31.12.2001 0:0:1 20.7.7478 0:20:20 21.7.7478 15:54:44 2.10.20749
Sample Output
0:0:0 1.1.0 4:23:72 26.5.0 0:8:48 58.2.146 9:99:98 31.8.0 0:0:1 100.10.2000 0:14:12 1.1.2001 6:63:0 7.3.6848
Source
AC代码:
#include<stdio.h> #include<string.h> int leap[50010]; //int isRunNian(int year); int DiJiTian(int year,int month,int day); void is_leap() { int i,j; memset(leap,0,sizeof(leap)); for(i=2000;i<=50000;i++) { if(i%400==0||(i%4==0&&i%100!=0)) leap[i]=1; else leap[i]=0; } } int main() { int hour,minute,second,day,month,year; int mhour,mmin,msec,mday,mmonth,myear; char c1,c2,c3,c4,c5; int s; is_leap(); scanf("%d",&s); while(s--) { scanf("%d%c%d%c%d%c%d%c%d%c%d",&hour,&c1,&minute,&c2,&second,&c3,&day,&c4,&month,&c5,&year); int i,sum=0; for(i=2000;i<year;i++) { if(leap[i]) sum+=366; else sum+=365; } sum += DiJiTian(year,month,day); myear=sum/1000; sum%=1000; mmonth=sum/100+1; sum%=100; mday=sum+1; sum=(hour*3600+minute*60+second)*125/108;// 题意是说一天的时间是一样的 ,所以100000/(24*3600)=125/108 mhour=sum/10000; sum%=10000; mmin=sum/100; sum%=100; msec=sum; printf("%d%c%d%c%d%c%d%c%d%c%d\n",mhour,c1,mmin,c2,msec,c3,mday,c4,mmonth,c5,myear); } return 0; } //int isRunNian(int year) //{ // if(year%400==0||(year%4==0&&year%100!=0)) // return 1; // else // return 0; //} int DiJiTian(int year,int month,int day)//求当前这已经过了多少天 { int i,sum=0; for(i=1;i<month;i++) { if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) sum+=31; else if(i==4||i==6||i==9||i==11) sum+=30; else if(i==2) { if(leap[year]) sum+=29; else sum+=28; } } sum+=day-1; return sum; }
只是调用了一个函数,就超时,没啥
超时代码:
#include<stdio.h> int isRunNian(int year); int DiJiTian(int year,int month,int day); int main() { int hour,minute,second,day,month,year; int mhour,mmin,msec,mday,mmonth,myear; char c1,c2,c3,c4,c5; int s; scanf("%d",&s); while(s--) { scanf("%d%c%d%c%d%c%d%c%d%c%d",&hour,&c1,&minute,&c2,&second,&c3,&day,&c4,&month,&c5,&year); int i,sum=0; for(i=2000;i<year;i++) { if(isRunNian(i)) sum+=366; else sum+=365; } sum += DiJiTian(year,month,day); myear=sum/1000; sum%=1000; mmonth=sum/100+1; sum%=100; mday=sum+1; sum=(hour*3600+minute*60+second)*125/108;// 题意是说一天的时间是一样的 ,所以100000/(24*3600)=125/108 mhour=sum/10000; sum%=10000; mmin=sum/100; sum%=100; msec=sum; printf("%d%c%d%c%d%c%d%c%d%c%d\n",mhour,c1,mmin,c2,msec,c3,mday,c4,mmonth,c5,myear); } return 0; } int isRunNian(int year) { if(year%400==0||(year%4==0&&year%100!=0)) return 1; else return 0; } int DiJiTian(int year,int month,int day)//求当前这已经过了多少天 { int i,sum=0; for(i=1;i<month;i++) { if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) sum+=31; else if(i==4||i==6||i==9||i==11) sum+=30; else if(i==2) { if(isRunNian(year)) sum+=29; else sum+=28; } } sum+=day-1; return sum; }
标签:
原文地址:http://blog.csdn.net/qq_16767427/article/details/44947431