一个有意思的小算法

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

    他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如: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;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值