码迷,mamicode.com
首页 > 其他好文 > 详细

2008上交:Day of Week

时间:2016-07-04 22:07:54      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

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;
}

 

2008上交:Day of Week

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/5641745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!