码迷,mamicode.com
首页 > 其他好文 > 详细

A - Calendar

时间:2014-07-16 17:21:40      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

A - Calendar
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

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
//不知为何wa了,泪奔。。。。。
#include<iostream>
#include <algorithm> 
#include<iomanip>
using namespace std;
int rn(int n)
{
	int l=0;
	if(n%4==0&&n%100!=0||n%400==0)
		l=1;
	return l;
}
char week[7][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int month[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31};
int main()
{
	
	int n,i,j,s,m,d,m1;
	while(cin>>n&&n!=-1)
	{
		d=(n)%7;
		j=0;
		n++;
		s=2000;
		m1=1;
		if(n==366)
			j++;
	
		while(n>365+j)
		{
			
			n=n-365-rn(s);
			s++;
			j=0;
		}
        if(n>59)
			n=n-rn(s);
		
		while(n>0)
		{
			n-=month[0][m1];
			m1++;
		}
			m1--;
		n+=month[0][m1];
	
        
		cout<<s<<'-';
		cout<<setw(2)<<setfill('0')<<m1<<'-'<<setw(2)<<setfill('0')<<n<<' ';
		cout<<week[d]<<endl;;

  
	}


return 0;
}

//AC
#include<stdio.h>
int main(){
    char w[7][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ,"Saturday"};
    int m[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
                  0,31,29,31,30,31,30,31,31,30,31,30,31};
    int yd[2]={365,366};
    long day;
    int year,month,week;
    int i,j,flag;
    while(scanf("%ld",&day)&&-1!=day){
        week=(day+6)%7;//得到星期几
        year=2000;
        flag=(0==year%4&&year%100!=0)||0==year%400;//flag=1为闰年
        ++day;//题目说经过多少天,所以在这里先加1
        for(;day>yd[flag];){//得到年份、剩余天数
            day-=yd[flag];
            year++;
            flag=(0==year%4&&year%100!=0)||0==year%400;
        }
        for(month=1;day>m[flag][month];++month){//得到月份和对应天数
            day-=m[flag][month];
        }
        printf("%d-%02d-%02d %s\n",year,month,day,w[week]);//%02d很方便
    }
}







A - Calendar,布布扣,bubuko.com

A - Calendar

标签:des   style   blog   http   color   strong   

原文地址:http://blog.csdn.net/rememberautumn/article/details/37811469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!