题目描述
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
#include
#include
char moname[13][20]=
{
"nothing","January","February","March","April","May","June",
"July","August","September","October","November","December"
};
char week[8][20]=
{
"nothing","Monday","Tuesday","Wednesday","Thursday" ,"Friday","Saturday","Sunday"
};
int days[13][2]= //平年和闰年每月天数
{
{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},
{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}
};
int leapyear(int year);
int Dvalue(int data1,int data2);
int main(void)
{
int day,month,year;
char str[20]; //存放月份
int data0=20010101; //2001.01.01 是星期一 以此为参照标准
int data; //输入的日期
int cnt; //差值
while(scanf("%d%s%d",&day,str,&year)!=EOF){
cnt=0;
for(int i=1;i<=12;i++){
if(strcmp(str,moname[i])==0){
month=i;
break;
} //找到,将月份对应数字赋给month
} //for
// 现在 day month year 均有了 计算日期差值
data=year*10000+month*100+day; //输入的日期
cnt=Dvalue(data0,data);
cnt%=7;
cnt=(cnt+7)%7;
printf("%s\n",week[cnt+1]);
} //while
return 0;
}
int Dvalue(int data1,int data2){
int cnt=0;
int year1,year2;
int month1,month2;
int day1,day2;
int temp;
int sign=1; //0 cnt负 1 cnt正
if(data1>data2){
temp=data2;
data2=data1;
data1=temp;
sign=0; //sign=0即输入小于2001010 cnt应为负数
} //now data2>=data1
year1=data1/10000;
year2=data2/10000;
data1%=10000;
data2%=10000;
month1=data1/100;
month2=data2/100;
day1=data1%100;
day2=data2%100;
while(year1!=year2||month1!=month2||day1!=day2){
day1++;
cnt++;
if(day1>days[month1][leapyear(year1)]){
day1=1; //到了下一月的第一天
month1++;
}
if(month1>12){ //到了下一年的第一天
day1=1;
month1=1;
year1++;
}
} //while
if(sign==0){
cnt=-cnt; //负的 cnt取负
}
return cnt;
}
int leapyear(int year){
int ret;
if(year%100==0){
if(year%400==0){
ret=1; //能被100 整除也能被400整除
}else{
ret=0; //能被100整除 不能被400整除
}
}else if(year%4==0){
ret=1; //不能被100整除 能被4整除
}else{
ret=0; //都不能
}
return ret;
}
晕晕晕,写了好久…
上一题是计算日期差值,就想着用上一题的方法算出差值,找个标准来计算对应的星期几就好了,写完之后错误,就发现日期小于标准的话要处理一下,稍微改一下对应规则
然后还是不对!就开始对着日历从1900到2050随机选几天来试,本地运行都对了,提交上去还是答案错误,彻底懵逼了,明明怎么试怎么对啊…
然后发现输出少加了\n…
话说我为什么要把自己丑陋又愚蠢的代码发出来,那个leapyear人家一行写完了,我写了一堆还懒得去改…
因为确信这么垃圾是不会被看到的:)