现在的位置: 首页 > 综合 > 正文

高斯日记(递归)

2014年07月27日 ⁄ 综合 ⁄ 共 1778字 ⁄ 字号 评论关闭

在竞赛上看到的一个题目,用递归写的。

1、题目标题高斯日记(满分4分)

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

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

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

高斯出生于:1777430日。

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

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

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

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



#include "stdio.h"

inttime,daycount;//time代表找到目标年份之后剩余的天数。daycount代表找到目标月份之后剩余的天数
int yearday(int temp)//判断平年闰年有多少天
{
if(temp@0==0||(temp%4==0&&temp0!=0))
return 366;
else return 365;
}
int monday(int year,int mon)//得到某年某月的天数
{
if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
return 31;
else if(mon==2)
{
return yearday(year)==366?29:28;
}
else return 30;
}
int getcount(int year)//得到原始日期开始到这一年结束的剩余天数
{
int count=0,i;
for(i=5;i<=12;++i)
count+=monday(year,i);
return count;
}
int getyear(int an,int count,int t)//获得目标年份
{
if(count>=an)
{
time=yearday(1777+t-1)-count+an;
return 1777+t-1;
}
else
{
count=count+yearday(1777+t);
getyear(an,count,t+1);
}
}
int getmon(int yearday,int time,int count,int t)//获得目标月份
{
if(count>=time)
{
daycount=monday(yearday,t-1)-(count-time);
return t-1;
}
else
{
count=count+monday(yearday,t);
getmon(yearday,time,count,t+1);
}
}
void main()
{
int an,year,mon,day;
an=8113;
year=getyear(an,getcount(1777),1);
mon=getmon(yearday(year),time-1,0,1);
 printf("%d-%d-%d",year,mon,daycount); 
}


#include <iostream>
using namespace std;
bool isTrue(int year)
{//判断闰年
   if(year%400==0) return true;
   if(year%4==0&&year%100!=0) return true;
   return false; 
}
int main()
{
    int n,year=1778,temp=31*5+30*3+1;
    cin>>n;
    n-=temp;
    int count=0;
    while(count<n)
    {
       if(isTrue(year))
          count+=366;
       else  count+=365;
       year++;
    }
    if(isTrue(--year))
          count-=366;
    else  count-=365;
    count=n-count;
    int i=1;
    int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    if(isTrue(year))
       a[2]=29;
    else a[2]=28;
    while(count>a[i])
    {
       count-=a[i];
       i++;
    }
    cout<<year<<"-"<<i<<"-"<<count<<endl;
    system("pause");
    return 0;   
}

抱歉!评论已关闭.