2008上交:Day of Week

题目描述:

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入:
9 October 2001
14 October 2001
样例输出:
Tuesday
Sunday

思路:先2016年7月3日(正好为星期日)为基准。求两日期之间的差值时可分别求两年份与1000年1月1日之间的距离,再做差。
复制代码
#include <iostream>
#include <string.h>
using namespace std;
char Month[13][20]={"Null","January","February","March","April","May","June","July","August","September","October","November","December"};
char Week[8][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int mds[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int d,y,m;
char mon[20];
int sMon(char s[])
{
    for(int i=1;i<=12;i++)
    {
        if(strcmp(Month[i],s)==0)
        {
            return i;
        }
    }
}
bool isLeap(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
    {
        return true;
    }
    return false;
}
int caldays(int year)
{
    int sum=0;
    for(int i=1000;i<=year;i++)
    {
        if(isLeap(i))
        {
            sum+=366;
        }
        else    sum+=365;
    }
    return sum;
}
int standard;
int main()
{
    standard+=caldays(2015);
    for(int i=1;i<=6;i++)
    {
        standard+=mds[i];
    }
    if(isLeap(2016))    standard+=1;
    standard+=3;
    while(cin>>d>>mon>>y)
    {
        m=sMon(mon);
        int days=0;
        days+=caldays(y-1);//本年不算 
        for(int i=1;i<m;i++)//本月不算 
        {
            days+=mds[i];
        }
        if(m>2&&isLeap(y))    days+=1;
        days+=d;
        int diff=days-standard;
        if(diff>=0)
        {
            diff%=7;
            cout<<Week[diff]<<endl;
        }
        else
        {
            diff=-diff;
            diff%=7;
            if(diff!=0)    diff=7-diff;
            cout<<Week[diff]<<endl;
        }
    }    
    return 0;
}
复制代码

 

posted on   vCoders  阅读(261)  评论(0编辑  收藏  举报

编辑推荐:
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
阅读排行:
· 对象命名为何需要避免'-er'和'-or'后缀
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· SQL Server如何跟踪自动统计信息更新?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示