天天看点

hdu 2133What day is it

What day is it

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2472    Accepted Submission(s): 720

Problem Description Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

Input There are multiply cases.

One line is one case.

There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

Output Output one line.

if the date is illegal, you should output "illegal". Or, you should output what day it is.

Sample Input

2007 11 17
        

Sample Output

Saturday
        

Author LGX  

Source HDU 2007-11 Programming Contest_WarmUp  

Recommend 威士忌  

#include<stdio.h>
int is_leap(int year)// 判断是否闰年
{
	if(year % 100 != 0 && year % 4 == 0 || year % 400 == 0)
		return 1;
	else return 0;
}
int main()
{
	int y, m, d, sum, i;
	while(~scanf("%d%d%d", &y, &m, &d))
	{
		sum = 0;
		if( y<=0 || y>=10000 || m<=0 || m>12 || d<=0 || d>31)
		{
			printf("illegal\n");
			continue;
		}
		if(is_leap(y) && m == 2 && d > 29)
		{
			printf("illegal\n");
			continue;
		}
		if(!is_leap(y) && m == 2 && d > 28)
		{
			printf("illegal\n");
			continue;
		}
		if((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
		{
			printf("illegal\n");
			continue;
		}
		sum += (y-1) * 365;
		for(i = 1; i < y; i++)
			if(is_leap(i))
				sum++;
		for(i = 1; i < m; i++)
			if(i == 1 || i == 3 || i == 5|| i == 7 || i == 8 || i== 10 || i == 12)
				sum += 31;
			else if(i == 4 || i == 6 || i == 9 || i == 11)
				sum += 30;
		if(is_leap(y) && m > 2)
			sum++;
		sum += d;
		sum %= 7;
		if(sum == 0)
			printf("Sunday\n");
		else if(sum == 1)
			printf("Monday\n");
		else if(sum == 2)
			printf("Tuesday\n");
		else if(sum == 3)
			printf("Wednesday\n");
		else if(sum == 4)
			printf("Thursday\n");
		else if(sum == 5)
			printf("Friday\n");
		else if(sum == 6)
			printf("Saturday\n");
	}
	return 0;
}
           

附上别人用蔡勒公式求的程序:

/*** 蔡勒公式,求某一天是周几(hdu 2133)***/
 #include <cstdio>
 
 const char *ans[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
 int isLeap(int y) { // 判断是否闰年
     return !(y % 4) && (y % 100) || !(y % 400);
 }
 
 int mod(int a, int b) { // 保证取模后是正数
     return (a % b + b) % b;
 }
 int main() {
     int y, m, d, c, w;
 
     while (~scanf("%d%d%d", &y, &m, &d)) {
         if (d <= 0 || m <= 0 || d >= 10000 || m > 12 || d > 31) {
             printf("illegal\n");
             continue;
         }
         if (!isLeap(y) && m == 2 && d > 28) {
             printf("illegal\n");
             continue;
         }
         if (isLeap(y) && m == 2 && d > 29) {
             printf("illegal\n");
             continue;
         }
         if ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) {
             printf("illegal\n");
             continue;
         }
         if (m == 1 || m == 2) m += 12, y--;
         /*** 蔡勒公式 ***/
         c = y / 100;
         y %= 100;
         w = (c >> 2) - (c << 1) + y + (y >> 2) + 13 * (m + 1) / 5 + d - 1;
         printf("%s\n", ans[mod(w, 7)]);
         /****************/
     }
     return 0;
 }