当前位置: 首页 > 工具软件 > leap-day > 使用案例 >

KY108 Day of Week

刘棋
2023-12-01

描述

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 2005, 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. 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

示例1

输入:

9 October 2001
14 October 2001

输出:

Tuesday
Sunday
思路:已知0001年1月1日为周一,可以转换为输入日期与0001年1月1日的日期差值来做,将差值对7取余即可。
示例代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int daytab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,13},
                   {0,31,29,31,30,31,30,31,31,30,31,30,13}};
vector<string>monthtab({"0","January","February","March","April","May","June","July","August","September","October","November","December"});
vector<string>week({"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});
bool IsLeapYear(int year)//判断是否为闰年
{
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int NumberOfYear(int year)//该年份的天数
{
    return 365+IsLeapYear(year);
}
int Number(int year,int month,int day)//计算输入的日期与0000年1月1日的差值
{
    int number=0;
    while(month--)
    {
        int row=IsLeapYear(year);
        number+=daytab[row][month];
    }
    while(year--)
    {
        number+=NumberOfYear(year);
    }
    number+=day;
    return number;
}
int main()
{
    int day,year;
    while(cin>>day)//0001年01月01日为周一
    {
        int month,number;
        string mon;
        cin>>mon;
        cin>>year;
        for(int i=1;i<monthtab.size();i++)
            if(mon==monthtab[i])
                month=i;
        number=Number(year,month,day)-367;//该日期与0000年1月1日的差值减去367即为该日期与0001年1月1日的差值
        number%=7;
        cout<<week[number]<<endl;
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答