1504 - Day of Week

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.

For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

题目输入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

题目输出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

输入/输出样例

输入格式

21 December 2012
5 January 2013

输出格式

Friday
Saturday

C语言解答

#include <stdio.h>
#include <math.h>
int main(void){
	char month[20][20]={" ","January","February","March","April","May","June","July","August","September","October","November","December"};
	int n,i;
	int day,year;
	char M[20];
	int w,y,c;
	char week[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	while (scanf("%d %s %d",&day,M,&year)!=EOF){
	for (i=1; i<13; i++){
		if (strcmp(M,month[i])==0){
			if(i<3){
				year-=1;
				i+=12;
			}
			break;
		}
		}
			c=year/100;
			y=year-100*c;
			w=(int)(c/4)-2*c+(int)(y+y/4)+(int)(13*(i+1)/5)+day-1;
			while(w<0){
				w+=7;
			}
			w=w%7;
			printf("%s\n",week[w]);
	}
	return 0;
}

C++解答

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;

int weekday(int d,int m,int y)
{
	int tm=m>=3?(m-2):(m+10);
	int ty=m>=3?y:(y-1);
	return (ty+ty/4-ty/100+ty/400+(int)(2.6*tm-0.2)+d)%7;
}

int main()
{
	char week[][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	int d,y,m;
	string M;
	while(cin>>d>>M>>y)
	{
		if(M=="January")
			m=1;
		else if(M=="February")
			m=2;
		else if(M=="March")
			m=3;
		else if(M=="April")
			m=4;
		else if(M=="May")
			m=5;
		else if(M=="June")
			m=6;
		else if(M=="July")
			m=7;
		else if(M=="August")
			m=8;
		else if(M=="September")
			m=9;
		else if(M=="October")
			m=10;
		else if(M=="November")
			m=11;
		else if(M=="December")
			m=12;
		printf("%s\n",week[weekday(d,m,y)]);
	}
	return 0;
}

Python解答

# coding=utf-8
import datetime
Date = datetime.datetime

while True:
    date = Date.strptime(input(),"%d %B %Y")
    print(date.strftime("%A"))