天天看點

7-題目1043:Day of Week

http://ac.jobdu.com/problem.php?pid=1043

題目描述:

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.

switch和case裡面不能用string,隻好用if解決,或者用map,一一對應

------------------------轉載内容------------------------http://bbs.bccn.net/thread-162951-2-1.html----------F12------------------

計算周幾?

公式中的符号含義如下,w:星期;c:世紀-1;y:年(兩位數);m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月來計算,比如2003年1月1日要看作2002年的13月1日來計算);d:日;[ ]代表取整,即隻要整數部分。(C是世紀數減一,y是年份後兩位,M是月份,d是日數。1月和2月要按上一年的13月和 14月來算,這時C和y均按上一年取值。)

算出來的W除以7,餘數是幾就是星期幾。如果餘數是0,則為星期日。

以2049年10月1日(100周年國慶)為例,用蔡勒(Zeller)公式進行計算,過程如下:

蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1

=49+[12.25]+5-40+[28.6]

=49+12+5-40+28

=54 (除以7餘5)

即2049年10月1日(100周年國慶)是星期5。

這個是最簡單的算法

蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

不過,以上公式隻适合于1582年10月15日之後的情形(當時的羅馬教皇将恺撒大帝制訂的儒略曆修改成格裡曆,即今天使用的公曆)。 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>  
#include<stdlib.h>
#include<iostream>  
#include<string>  
#include<map>
using namespace std;

int judge_366(int year)     //判斷閏年,預設公元紀年從1400開始,即輸入年份要大于1400年
{
	if ((year % 4 == 0  && year % 100 != 0 )|| year % 400 == 0)
		return 366;
	else 
		return 365;
}
int judge_31(int month, int year)   //判斷每月日數
{
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) return 31;
	else if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
	else
	{
		if (judge_366(year) == 366) return 29;
		else return 28;
	}
}
int E_to_Num(string month)   //将英語月份轉化成數字月份
{
	map<string, int> Map;
	Map["January"] = 1; Map["February"] = 2; Map["March"] = 3; Map["April"] = 4; Map["May"] = 5; Map["June"] = 6;
	Map["July"] = 7; Map["August"] = 8; Map["September"] = 9; Map["October"] = 10; Map["November"] = 11; Map["December"] = 12;
	return Map[month];
}
string Num_to_E(int week)    //将數字星期幾轉化成英語
{
	map<int, string> Map;
	Map[1] = "Monday"; Map[2] = "Tuesday"; Map[3] = "Wednesday"; Map[4] = "Thursday"; 
	Map[5] = "Friday"; Map[6] = "Saturday"; Map[7] = "Sunday";
	return Map[week];
}
int main()
{
	string month_E;
	int  year,month,day,sum,i;

	while (cin >> day >> month_E >> year)
	{
		month = E_to_Num(month_E);  //将char型日期轉化成int型

		sum = 0;
		for ( i = 1000; i < year; i++)   //把從1000年起的日子加起來,今年的日子單獨算
			sum += judge_366(i);
		for (i = 1; i < month; i++)
			sum += judge_31(i, year);
		sum += day;

		cout << Num_to_E((sum + 1) % 7 + 1)<< endl;
	}
	return 0;
}