|
Calendar
Description
A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years and centuries. The terms of hour, day, month, year and century are all units of time measurements of a calender system.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years. Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week. Input
The input consists of lines each containing a positive integer, which is the number of days that have elapsed since January 1, 2000 A.D. The last line contains an integer −1, which should not be processed.
You may assume that the resulting date won’t be after the year 9999. Output
For each test case, output one line containing the date and the day of the week in the format of "YYYY-MM-DD DayOfWeek", where "DayOfWeek" must be one of "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".
Sample Input 1730 1740 1750 1751 -1 Sample Output 2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday Source |
以前做关于日期的题目感觉就很烦,现在感觉还是挺简单的,只要不停的把所给的数字减去年份月份,同时记录年份和月份的变量加上相应的值,最后得到的就是最终日期了,至于周几 可以直接对7取余找规律。。
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int month1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
long long int n;
int main(){
while(cin>>n)
{
long long int n1=n;
int yy=2000;
int mm=1;
int dd=1;
if(n==-1)break;
while(n>=1)
{
if(yy%400==0||(yy%4==0&&yy%100!=0))
{
if(n>=366)
n-=366,yy++;
else {
while(n>=month1[mm])
{
n-=month1[mm];
mm++;
}
dd+=n;
n=0;
}
}
else {
if(n>=365)
n-=365,yy++;
else {
while(n>=month[mm])
{
n-=month[mm];
mm++;
}
dd+=n;
n=0;
}
}
}
//cout<<yy<<" "<<mm<<" "<<dd<<endl;
int ans=n1%7;
cout<<yy<<"-";
if(mm<10)cout<<"0"<<mm<<"-";
else cout<<mm<<"-";
if(dd<10)cout<<"0"<<dd<<" ";
else cout<<dd<<" ";
if(ans==2)cout<<"Monday"<<endl;
else if(ans==3)cout<<"Tuesday"<<endl;
else if(ans==4)cout<<"Wednesday"<<endl;
else if(ans==5)cout<<"Thursday"<<endl;
else if(ans==6)cout<<"Friday"<<endl;
else if(ans==0)cout<<"Saturday"<<endl;
else if(ans==1)cout<<"Sunday"<<endl;
}
return 0;
}

本文介绍了一个基于格里高利历的日期转换算法挑战,通过输入自2000年1月1日起的天数,计算并输出对应的日期及星期。文章提供了完整的C++实现代码,并解释了如何处理闰年和平年的差异。
470

被折叠的 条评论
为什么被折叠?



