您的位置:首页 > 其它

7-题目1043:Day of Week

2016-02-20 16:36 381 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: