12019 - Doom's Day Algorithm

本文介绍了一个基于Doomsday算法的解决方案,用于快速计算2011年任意给定日期对应的星期数。通过查找特定月份的基准日期(如4月4日等),并利用这些日期总是落在同一星期的事实,可以轻松确定任何日期所在的星期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Background

No. Doom's day algorithm is not a method to compute which day the world will end. It is an algorithm created by the mathematician John Horton Conway, to calculate which day of the week (Monday, Tuesday, etc.) corresponds to a certain date.

This algorithm is based in the idea of the doomsday, a certain day of the week which always occurs in the same dates. For example, 4/4 (the 4th of April), 6/6 (the 6th of June), 8/8 (the 8th of August), 10/10 (the 10th of October) and 12/12 (the 12th of December) are dates which always occur in doomsday. All years have their own doomsday.

In year 2011, doomsday is Monday. So all of 4/4, 6/6, 8/8, 10/10 and 12/12 are Mondays. Using that information, you can easily compute any other date. For example, the 13th of December 2011 will be Tuesday, the 14th of December 2011 will be Wednesday, etc.

The Problem

Other days which occur on doomsday are 5/9, 9/5, 7/11 and 11/7. Also, in leap years, we have the following doomsdays: 1/11 (the 11th of January) and 2/22 (the 22nd of Febrary), and in non-leap years 1/10 and 2/21.

Given a date of year 2011, you have to compute which day of the week it occurs.

Input

The input can contain different test cases. The first line of the input indicates the number of test cases.

For each test case, there is a line with two numbers: M D. M represents the month (from 1 to 12) and D represents the day (from 1 to 31). The date will always be valid.

Output

For each test case, you have to output the day of the week where that date occurs in 2011. The days of the week will be: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.

求2011年某天是星期几

Sample Input

8
1 6
2 28
4 5
5 26
8 1
11 1
12 25
12 31

Sample Output

Thursday
Monday
Tuesday
Thursday
Monday
Tuesday
Sunday
Saturday
解题思路:先暴力求出每月的第一天是星期几再,再来判断要求的日期是星期几
#include<stdio.h>
int main()
{int n,m,d,t=0,i,flag;
scanf("%d",&n);
while(n--){
           scanf("%d%d",&m,&d);
           switch(m){
                     case 1:t=6;break;
                     case 2:t=2;break;
                     case 3:t=2;break;
                     case 4:t=5;break;
                     case 5:t=7;break;
                     case 6:t=3;break;
                     case 7:t=5;break;
                     case 8:t=1;break;
                     case 9:t=4;break;
                     case 10:t=6;break;
                     case 11:t=2;break;
                     case 12:t=4;break;
                     }
           flag=(d+t-1)%7;
           if(flag==0)printf("Sunday\n");
           else if(flag==1)printf("Monday\n");
           else if(flag==2)printf("Tuesday\n");
           else if(flag==3)printf("Wednesday\n");
           else if(flag==4)printf("Thursday\n");
           else if(flag==5)printf("Friday\n");
           else if(flag==6)printf("Saturday\n");
           }
return 0;
}

 

转载于:https://www.cnblogs.com/EVA00/archive/2013/02/16/2913692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值