题目链接:
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1575
Calculate the day-1
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 123(64 users) Total Accepted: 90(62 users) Rating: Special Judge: No
Description
September 1st is a boring day, because this day is the beginning of the new semester. But we are curious about what day is September 1st in a week every year. Given the hypothesis of September 1st this year which suppose to be x(x belong to Monday, Tuesday and so on) in a week.Please calculate what September 1st is after n (n is an integer)years.
Input
Input a series of test date.
Each line including a string x and an integer n.(n<100)
Output
Output a string, said what September 1st is after n years.
Sample Input
Saturday 12
Monday 10
Sample Output
Sunday
Saturday
Hint
Assuming this year is a leap year.
Author
孟祥凤@Amber
【思路分析】题目给说了n<100,所以难度降低了不少。然后一开始的时候是闰年,那么只考虑四年一闰就好了,暴力扫一遍,看到n年之后的9月1号总共过了多少天,然后根据现在的9月1号是星期几求n年之后的9月1号是星期几。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
char str[10];
int main()
{
int n;
while(~scanf("%s %d",str,&n))
{
int sum=0;
for(int i=1; i<=n; i++)
{
if(i%4==0)
{
sum+=366;
}
else
{
sum+=365;
}
}
for(int i=0; i<7; i++)
{
if(strcmp(a[i],str)==0)
{
sum+=(i+1);
break;
}
}
if(sum%7==0)
{
printf("Sunday\n");
}
else
{
printf("%s\n",a[sum%7-1]);
}
}
return 0;
}