codeup——问题 B: Day of Week(按照蔡勒公式写)

问题 B: Day of Week
时间限制: 1 Sec  内存限制: 32 MB
提交: 566  解决: 161
[提交][状态][讨论版][命题人:外部导入]
题目描述
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

蔡勒公式详见百度,还可以查到推导过程,古人的智慧ヾ(๑╹◡╹)ノ"

这里需要注意的是: 负数不能按习惯的余数的概念求余数,只能按数论中的余数的定义求余。为了方便计算,我们可以给它加上一个7的整数倍,使它变为一个正数,比如加上70,得到55。再除以7,余6,说明这一天是星期六。

#include 
#include 
#include 

int month[13][2] = { { 0, 0 }, { 31, 31 }, { 28, 29 }, { 31, 31 }, { 30, 30 }, { 31, 31 }, { 30, 30 }, { 31, 31 }, { 31, 31 }, { 30, 30 }, { 31, 31 }, { 30, 30 }, { 31, 31 } };
bool isleap(int year){//判断是否是闰年
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int change(char month[20]){//判断英文单词对应的月份的数字
	int m;
	if (month[0] == 'F') m = 14;
	if (month[0] == 'S') m = 9;
	if (month[0] == 'O') m = 10;
	if (month[0] == 'N') m = 11;
	if (month[0] == 'D') m = 12;
	if (month[0] == 'M'){
		if (month[2] == 'r') m = 3;
		else m = 5;
	}
	if (month[0] == 'J'){
		if (month[1] == 'a') m = 13;
		else if (month[2] == 'n') m = 6;
		else if (month[2]=='l') m = 7;
	}
	if (month[0] == 'A') {
		
		if (month[2] == 'r') m = 4;
		else m = 8;
	}
	return m;
}
int main(){
	int d1, y1;
	char cm1[20];
	while (scanf("%d %s %d", &d1, cm1, &y1) != EOF){
		int y=0, c=0, m=0, d=0,w=0;
		m = change(cm1);
		
		if (m > 12){
		c = (y1-1) / 100;
		y = (y1-1)%100;
	
		} 
		else {
			c = y1 / 100;
			y = y1 % 100;
			
		}
		
		d = d1;
	
		
			w = y + int(y / 4) + int(c / 4) - 2 * c + int((26 * (m + 1)) / 10) + d - 1;
		
		int today=0;
		while (w<0){
			
			w = w + 7;
		}
		
		today = w % 7;
		
		
		switch (today){
		case 1:
			printf("Monday\n");
			break;
		case 2:
			printf("Tuesday\n");
			break;
		case 3:
			printf("Wednesday\n");
			break;
		case 4:
			printf("Thursday\n");
			break;
		case 5:
			printf("Friday\n");
			break;
		case 6:
			printf("Saturday\n");
			break;
		case 0:
			printf("Sunday\n");

		}
	}
	system("pause");//提交的时候要注销掉
	return 0;
}

 

你可能感兴趣的:(codeup)