ac代码
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<time.h>
using namespace std;
/*
20130101
20130105
20130205
20130101
20000101
20010105
20130101
20140105
*/
int month[13][2]={
{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}
};
int year[2]={365,366};
int isLeap(int year) {
return ((year%400==0)||(year%4==0 && year%100!=0));
}
int main()
{
int date1,date2;
int y1,m1,d1,tmp,y2,m2,d2;
while(cin>>date1>>date2) {
if((date1-date2)>0){
tmp=date1;
date1=date2;
date2=tmp;
}
y1=date1/10000;
tmp=date1-y1*10000;
m1=tmp/100;
d1=(tmp-m1*100)%100;
// cout<<y1<<endl<<m1<<endl<<d1<<endl;
y2=date2/10000;
tmp=date2-y2*10000;
m2=tmp/100;
d2=(tmp-m2*100)%100;
int ans=1;
while((y1<y2)||(m1<m2)||(d1<d2)) {
d1++;
if(d1==month[m1][isLeap(y1)]+1) {
m1++;
d1=1;
}
if(m1==13) {
y1++;
m1=1;
}
ans++;
}
cout<<ans<<endl;
}
}
测试数据
1. day
20130101
20130105
ans: 5
2. month
20130205
20130101
ans: 36
3. year(isleap)
20000101
20130105
ans: 371
4. year(isNotleap)
20130101
20140105
ans: 370
数据如下:
20130101
20130105
20130205
20130101
20000101
20010105
20130101
20140105
刚开始一直运行超限0(运行超时),但是查了下时间复杂度,也和别人的ac代码进行了对比,感觉问题并不是出在while循环中。
但是修改了一下判断闰年的函数int isLeap(int year)
(我以为只有能整除400才是闰年,经典缺乏常识了),就能过了。
真是人间大迷惑= =|||