1199: 英雄无敌3(2)
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 306 Solved: 53
[Submit][Status][Web Board]
Description
英雄无敌3中的兽人觉得人类的星期制度很好,要模仿这个制度,但他们大多都很愚蠢,他们搞不明白人类的星期制度。虽然他们出了很多题目(给出日期,输出星期几),但不知道答案,作为兽人的人类朋友,你可以帮助他们吗?(为了表示方便,0代表星期天,1代表星期一,2代表星期二,3代表星期三,4代表星期四,5代表星期五,6代表星期六)
Input
第一行输入一个数t,代表题目数。
每个题目有3个正整数分别为y(0 < y < = 2013), m(0 = < m<=20), d(0<=d<=40)分别代表年,月,日。
Output
对于每组案例,输出一个整数,代表星期几(当然由于兽人很愚蠢,他们给出的日期有可能是不存在的,对于这种情况,请输出-1)
Sample Input
1
2013 4 25
Sample Output
4
HINT
Source
蔡勒公式
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
int main()
{
int t, year, mouth, day;
scanf("%d", &t);
while (t--)
{
int m[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
scanf("%d%d%d", &year, &mouth, &day);
if (mouth == 0 || mouth>12 || day == 0 || day>31)
{
printf("-1\n");
continue;
}
if (!((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
m[2] = 28;
}
if (day>m[mouth])
{
printf("-1\n");
continue;
}
if (mouth<3)
{
year--;
mouth += 12;
}
int c = year / 100;
int y = year % 100;
printf("%d\n", ((c / 4 - 2 * c + y + y / 4 + 13 * (mouth + 1) / 5 + day - 1) % 7 + 7) % 7);
}
return 0;
}