高斯日记

大数学家高斯有个好习惯:无论如何都要记日记。

他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210

后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?

高斯出生于:1777年4月30日。

在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。

高斯获得博士学位的那天日记上标着:8113

请你算出高斯获得博士学位的年月日。

 1 #include<stdio.h>
 2 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 3 struct date
 4 {
 5     int year,month,day;
 6 };
 7 int leap(int year)//判闰年
 8 {
 9     return (year%4==0&&year%100!=0)||year%400==0;
10 }
11 int date2int(date a)//日期转天数偏移
12 {
13     int ret=a.year*365+(a.year-1)/4-(a.year-1)/100+(a.year-1)/400,i;
14     days[1]+=leap(a.year);
15     for(i=0;i<a.month-1;ret+=days[i++]);
16     days[1]=28;
17     return ret+a.day;
18 }
19 date int2date(int a)//天数偏移转日期
20 {
21     date ret;
22     ret.year=a/146097*400;
23     for (a%=146097;a>=365+leap(ret.year);a-=365+leap(ret.year),ret.year++);
24     days[1]+=leap(ret.year);
25     for (ret.month=1;a>=days[ret.month-1];a-=days[ret.month-1],ret.month++);
26     days[1]=28;
27     ret.day=a+1;
28     return ret;
29 }
30 int main()
31 {
32     date a,b;//定义了俩个struct date的变量
33     int i,n,m;
34     while(scanf("%d",&n)!=-1)
35     {
36         a.year=1777;
37         a.month=4;
38         a.day=30;
39         m=date2int(a)+n-1;
40         b=int2date(m);
41         printf("%d-%d-%d\n",b.year,b.month,b.day);
42     }
43     return 0;
44 }

别人的代码

posted @ 2013-11-14 13:37  栋先生  阅读(438)  评论(0)    收藏  举报