题目1043:Day of Week(输入日期与当前日起天数差%7,在做相关星期调整)

题目描述:

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

提示:

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

代码:

# include<iostream>
using namespace std;
 
# include<string.h>
 
int main()
{
    char month[13][10] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    char week[7][10] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int dOFm[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int y, m, d;
    int cy = 2014, cm = 7, cd = 24, cw = 4;//这个要更换成今天的具体情况
    char mon[10];       
    int i, count1, count2, dOFweek;
 
    while (cin >> d >> mon >> y)
    {
        for (i = 1; i <= 12; i++)
        {
            if (strcmp(month[i], mon) == 0)
            {
                m = i;
                break;
            }               
        }
        //cout << m << endl;
 
        if (y > cy || (y == cy&&m > cm) || (y == cy&&m == cm&&d > cd))//输入的年月日在今天之后
        {
            //计算cy cm cd离cy 01 01的天数
            count1 = 0;
            for (i = 1; i < cm; i++)
            {
                count1 += dOFm[i];
            }
            if (cm>2 && ((cy % 4 == 0 && cy % 100 != 0) || cy % 400 == 0))
            {
                count1 += 1;
            }
            count1 += cd;
 
            //计算y m d离cy 01 01的天数
            count2 = 0;
            for (i = cy; i < y; i++)
            {
                if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
                {
                    count2 += 366;
                }
                else
                {
                    count2 += 365;
                }
            }
            for (i = 1; i < m; i++)
            {
                count2 += dOFm[i];
            }
            if (m>2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
            {
                count2 += 1;
            }
            count2 += d;
 
            //cout << count2 - count1 << endl;
            dOFweek = ((count2 - count1) % 7 + cw) % 7;
            cout << week[dOFweek] << endl;
        }
        else//输入的年月日在今天之前
        {
            //计算y m d离y 01 01的天数
            count1 = 0;
            for (i = 1; i < m; i++)
            {
                count1 += dOFm[i];
            }
            if (m>2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
            {
                count1 += 1;
            }
            count1 += d;
 
            //计算cy cm cd离y 01 01的天数
            count2 = 0;
            for (i = y; i < cy; i++)
            {
                if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
                {
                    count2 += 366;
                }
                else
                {
                    count2 += 365;
                }
            }
            for (i = 1; i < cm; i++)
            {
                count2 += dOFm[i];
            }
            if (cm>2 && ((cy % 4 == 0 && cy % 100 != 0) || cy % 400 == 0))
            {
                count2 += 1;
            }
            count2 += cd;
 
            //cout << count2 - count1 << endl;
            dOFweek = ((cw - (count2 - count1) % 7) + 7) % 7;
            cout << week[dOFweek] << endl;
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1043
    User: mmcNuaa@163.com
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/
View Code

 

posted @ 2014-07-25 22:49  mmcmmc  阅读(274)  评论(0编辑  收藏  举报