一个有意思的小算法
来源:互联网 发布: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;}
- 一个有意思的小算法
- 有意思的一个算法。
- 一个有意思的算法
- 一个有意思的小细节
- 一个有意思的排序算法
- 非常有意思的一个小时钟
- 一个有意思的题-小猴子下落
- 分享一个有意思的小程序!!
- 很有意思的一个小问题
- 分享一个有意思的英语小故事
- 数学家高斯的问题,一个有意思的小算法,根据高斯整数计算日期
- 一个很有意思的排序算法
- 记录一个小有意思的改变路径的问题
- java面试时遇到的一个有意思的小问题
- 有意思的小笑话
- 有意思的小程序
- 有意思的小程序
- 朋友给的一个有意思Java小程序
- Android基于XMPP Smack openfire 开发的聊天室(四) 【创建房间、表单;报文】
- HDU 4790 2013 ACM/ICPC 成都赛区现场赛 J. Just Random
- struts2与Servlet解耦
- Android基于XMPP Smack openfire 开发的聊天室(五) 【邀请、被邀请】
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现
- 一个有意思的小算法
- Android基于XMPP Smack openfire 开发的聊天室(六) 【加入房间、权限错误】
- You Are My Brother
- Android基于XMPP Smack openfire 开发的聊天室(七) 【成员状态、自身状态】
- FZU1759 Super A^B mod C 欧拉函数+快速幂+公式
- c# 报表MicrosoftReportViewer
- Deep Learning论文笔记之(五)CNN卷积神经网络代码理解
- Oracle中存储过程的使用
- 连连看判定图形消除小算法