#include<iostream>
using namespace std;
//测试是否闰年，计算日期是第几日(days(0,1,1)==1)，计算日期是星期几，
//全部复杂度都是O(1)
//**************************
int md[]={0,0,31,28,31,30,31,30,31,31,30,31,30};

inline bool isly(int y){//isLeapYear,测试是否闰年
return y%400==0 || y%100 !=0 && y%4==0;
}

inline int days(int y, int m, int d){//返回日期序列化值days(0,1,1)==1;
d+=m<=2 && isly(y) ? md[m]-1 : md[m];
d+=365*y+y/4-y/100+y/400+1;
return d;
}
inline int day(int y, int m, int d){//返回日期是星期几(0-日,1-一....)
return (days(y,m,d)+5)%7;
}
//**************************
void test();
int main(){
//初始化
int i;
for(i=2; i<=12;i++){
md[i]+=md[i-1];
}
//测试
int y1,m1,d1,y2,m2,d2;
//freopen("in.txt","r",stdin);
/*while(cin>>y1>>m1>>d1)
cout<<days(y1,m1,d1)<<endl;
while(cin>>y1>>m1>>d1>>y2>>m2>>d2)
cout<<days(y2,m2,d2)-days(y1,m1,d1)<<endl;/**/
//more test
test();
return 0;
}
#include<cassert>
void test(){
assert(days(0,1,1)==1);
assert(days(0,1,31)==31);
assert(days(0,2,1)==32);
assert(days(0,3,1)-days(0,2,28)==2);
assert(days(0,12,31)-days(0,1,1)==365);
assert(days(1,1,1)-days(0,12,31)==1);
assert(days(1,3,1)-days(1,2,28)==1);
assert(days(1,12,31)-days(0,12,31)==365);
assert(days(4,12,31)-days(3,12,31)==366);
assert(days(100,3,1)-days(100,2,28)==1);
assert(days(101,1,1)-days(100,1,1)==365);
assert(days(401,2,28)-days(400,2,28)==366);
assert(days(1000,3,1)-days(1000,2,28)==1);
assert(days(2000,3,1)-days(2000,2,28)==2);
assert(days(2000,2,1)-days(2000,1,30)==2);
assert(days(2100,3,1)-days(2100,2,28)==1);
assert(days(2000,4,1)-days(2000,3,30)==2);
assert(days(2000,5,1)-days(2000,4,30)==1);
assert(days(2000,6,1)-days(2000,5,30)==2);
assert(days(2000,7,1)-days(2000,6,30)==1);
assert(days(2000,8,1)-days(2000,7,30)==2);
assert(days(2000,9,1)-days(2000,8,30)==2);
assert(days(2000,10,1)-days(2000,9,30)==1);
assert(days(2000,11,1)-days(2000,10,30)==2);
assert(days(2000,12,1)-days(2000,11,30)==1);
assert(days(2001,1,1)-days(2000,12,30)==2);
assert(days(3000,12,31)==1096093);
assert(day(2006,6,14)==3);
assert(day(2008,3,1)==6);
assert(day(1985,10,3)==4);
assert(day(2006,1,1)==0);//Sunday.
cout<<"Accepted!"<<endl;

}

---

posted on 2006-06-14 15:18 踏雪赤兔 阅读(361) 评论(0)  编辑 收藏 引用 所属分类: 零件仓库

•

• 积分 - 392816
• 排名 - 10