题目描述
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.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday
根据日期判断出星期几的模板
int kk(int year,int month,int day)//根据日期判断出星期几
{
// 公式:W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 (其中[ ]为取整符号)
// w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
// c:世纪-1(前两位数)
// y:年(后两位数)
// m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
// d:日 [ ]代表取整,即只要整数部分
if(month==1||month==2)
{
month+=12;
year--;
}
int c=year/100;
int y=year%100;
int m=month;
int d=day;
int W=c/4-2*c+y+y/4+26*(m+1)/10+d-1;
if(W<0)
return (W+(-W/7+1)*7)%7;
W=W%7;
if (W == 0 ) //星期日不作为一周的第一天
W= 7;
return W;
}
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef struct data
{
int d;
char w[10];
int y;
}D;
int isyear(int n)
{
if(n%4==0&&n%100!=0||n%400==0)
return 1;
return 0;
}
int kk(int year,int month,int day);
int month[40]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char week[40][40]={{},{"January"},{"February"},{"March"},{"April"},{"May"},{"June"},{"July"},{"August"},{"September"},{"October"},{"November"},{"December"}};
char xxx[40][40]={{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},{"Friday"},{"Saturday"},{"Sunday"}};
int main()
{
int y0=1,m0=1,d0=1,w0=1;
int y,m,d;
D Data;
while(scanf("%d %s %d",&Data.d,Data.w,&Data.y)!=EOF)
{
int s=0;
int day=0;
for(int i=1; i<=12; i++)
{
if(strcmp(week[i],Data.w)==0)
{
m=i;break;
}
}
//cout<<m<<endl;
d=Data.d;
y=Data.y;
/*for(int i=y0; i<=y-1; i++)
{
if(isyear(i)==1) s=s+366;
else s=s+365;
}
if(isyear(y)==1) month[2]=29;
else month[2]=28;
for(int i=1; i<=m-1; i++)
{
s=s+month[i];
}
s=s+d-1;
day=(s+w0-1)%7;//cout<<day<<endl;
cout<<xxx[day]<<endl;*/
cout<<xxx[kk(y,m,d)-1]<<endl;
}
return 0;
}
int kk(int year,int month,int day)//根据日期判断出星期几
{
// 公式:W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 (其中[ ]为取整符号)
// w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
// c:世纪-1(前两位数)
// y:年(后两位数)
// m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
// d:日 [ ]代表取整,即只要整数部分
if(month==1||month==2)
{
month+=12;
year--;
}
int c=year/100;
int y=year%100;
int m=month;
int d=day;
int W=c/4-2*c+y+y/4+26*(m+1)/10+d-1;
if(W<0)
return (W+(-W/7+1)*7)%7;
W=W%7;
if (W == 0 ) //星期日不作为一周的第一天
W= 7;
return W;
}