计算星期几(基姆拉尔森公式)

题目描述

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

解题思路 

嗯...就是用一个公式 ,基姆拉尔森计算公式.

m 代表月份  d 代表日期  y代表年份

把当年的1月和2月当做上一年的13月和14月计算

公式    w= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 

所得的w:  0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六

另外,遇到年份月份这种问题,用数组写比较方便一点,用数组存放每月的天数.

代码

 

#include<stdio.h>
#include<string.h>
int leep[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
                {0,31,29,31,30,31,30,31,31,30,31,30,31}};
int chick(int year){
    if( ((year%4==0&&year%100!=0)||year%400==0) ) return 1;
    else return 0;
}
char s[10][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int main(){
    int y,m,d,w;

    while(~scanf("%d%d%d",&y,&m,&d)){
        if(m==0){
            printf("illegal\n");continue;
        }
        if(d<=0||d>leep[chick(y)][m]){
            printf("illegal\n");continue;
        }
        if(m==1||m==2){
            m+=12;y--;
        }
        w= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
        puts(s[w]);
    }
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值