一个有意思的小算法

来源:互联网 发布:JAVA求合法数字 编辑:程序博客网 时间:2024/05/22 08:24

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

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

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

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

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

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

提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21

 

 

(C++代码实现如下:)

 

#include "stdafx.h"#include <iostream>#define REST 245 using namespace std;enum YearState{LeapYear,CommonYear};void ymdcount(int number){   int year=1777,month=5,day=1;   YearState yearstate=CommonYear;   int c[12]={31,28,31,30,31,30,31,31,30,31,30,31};   int l[12]={31,29,31,30,31,30,31,31,30,31,30,31};   if(number>0&&number<=REST)   {while(true){if(number>c[month-1]) {number-=c[month-1];month++;}else break;}day=number;    cout<<year<<"-"<<month<<"-"<<day<<endl;   }   if(number>REST)   {year++;month=1;day=1;number=number-REST;    while(true)    {if(year%4==0&&(year%100!=0||year%400==0)){if(number>366){number=number-366;year++;}else {yearstate=LeapYear;break;}}    else{if(number>365){number=number-365;year++;}else {yearstate=CommonYear;break;}}}    switch(yearstate){case LeapYear:        while(true){if(number>l[month-1]) {number-=l[month-1];month++;}else break;}case CommonYear:    while(true){if(number>c[month-1]) {number-=c[month-1];month++;}else break;}}day=number;cout<<year<<"-"<<month<<"-"<<day<<endl;   }}int main(int argc, char* argv[]){int daycount;for(int i=0;;i++){cin>>daycount;if(daycount==0) break;else ymdcount(daycount);}getchar();return 0;}


 

 

 

原创粉丝点击