Problem Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?
Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
Sample Input
2007 11 17
Sample Output
Saturday
Author
LGX
这题一定要注意啊!逻辑错误也是硬伤!
首先我们把公元1年1月1日设为星期一
这是我原来的代码:
#include <stdio.h>
int isleap(int y)
{
if ((y%4==0&&y%100!=0)||y%400==0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int y,m,d,sum,flag;
int ms[20] = {0,31,28,31,30,31,30,31,31,30,31,30,31,0};
while (~scanf("%d%d%d",&y,&m,&d))
{
if (d > ms[m] || d < 1 || m < 1 || m > 12 || y <1 || y>10000)
{
printf("illegal\n");
}
else
{
sum=0;
for (int i=1;i<y;i++)
{
if (isleap(i))
{
sum+=366;
}
else
{
sum+=365;
}
sum%=7;
}
if (isleap(y))
{
ms[2]=29;
}
else
{
ms[2]=28;
}
for (int j=0;j<m;j++)
{
sum+=ms[j];
}
sum+=d;
//printf("%d\n",sum);
flag=sum%7;
switch (flag)
{
case 0: printf("Sunday\n");break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n");break;
case 4: printf("Thursday\n");break;
case 5:printf("Friday\n"); break;
case 6:printf("Saturday\n");break;
}
}
}
}
这是我现在的代码:
#include <stdio.h>
int isleap(int y)
{
if ((y%4==0&&y%100!=0)||y%400==0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int y,m,d,sum,flag;
int ms[20] = {0,31,28,31,30,31,30,31,31,30,31,30,31,0};
while (~scanf("%d%d%d",&y,&m,&d))
{
if (isleap(y))
{
ms[2]=29;
}
else
{
ms[2]=28;
}
if (d > ms[m] || d < 1 || m < 1 || m > 12 || y <1 || y>10000)
{
printf("illegal\n");
}
else
{
sum=0;
for (int i=1;i<y;i++)
{
if (isleap(i))
{
sum+=366;
}
else
{
sum+=365;
}
sum%=7;
}
for (int j=0;j<m;j++)
{
sum+=ms[j];
}
sum+=d;
//printf("%d\n",sum);
flag=sum%7;
switch (flag)
{
case 0: printf("Sunday\n");break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n");break;
case 4: printf("Thursday\n");break;
case 5:printf("Friday\n"); break;
case 6:printf("Saturday\n");break;
}
}
}
}
细心的你有没有发现我把
if (isleap(y))
{
ms[2]=29;
}
else
{
ms[2]=28;
}
移到前面去了,看到下一段代码你就知道了!
if (isleap(y))
{
ms[2]=29;
}
else
{
ms[2]=28;
}
if (d > ms[m] || d < 1 || m < 1 || m > 12 || y <1 || y>10000)
{
printf("illegal\n");
}
你要判断是否illegal中的
d > ms[m]你就应该先判断Y是否瑞年,因为测试数据在2月设下了关口!我的ms[]默认是28啊!而判断放在了上段代码后面,无法进行判断,这是硬伤啊!<img alt="大哭" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif" />