九度OJ 题目1043:Day of Week

时间:2022-10-21 12:45:27
题目描述:

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

来源:

2008年上海交通大学计算机研究生机试真题


代码:

# include <iostream>
# include <string.h>
# include <stdio.h>
using namespace std;
int is_runyear(int year)//判断是否闰年
{
if(year%400==0) return 1;
else if(year%4 ==0 && year%100 != 0) return 1;
else return 0;
}
struct dates
{
int day;
string month;
int year;
};
int main()
{
const string month_name[13]={"0","January","February","March","April","May","June","July","August","September","October","November","December"};
const string week_name[7] = {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday","Monday"};
struct dates date;
string weekday;
while(cin>>date.day>>date.month>>date.year&&date.year>=1000&&date.year<=3000)
{
int i,year,month,allday=0,weeknum;
int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
year = date.year-1000;
for(i=1;i<=12;i++)
{
if(month_name[i]==date.month) month=i;
}
for(i=1;i<=year;i++)
{
allday+= 365+is_runyear(i+999);
}
if(is_runyear(date.year)==1) month_day[2]=29;
for(i=0;i<month;i++)
{
allday+= month_day[i];
}
allday+=date.day;
weeknum = allday%7;
weekday = week_name[weeknum];
cout<<weekday<<endl;
}
}

/**************************************************************
Problem: 1043
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/