hdu 2133 日期的格式转化

本文介绍了一个通过输入年月日计算该日是星期几的算法,并提供了完整的C语言代码实现。文章考虑了各种边界情况,如闰年和平年的二月天数、大小月等,确保了算法的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What day is it

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2338    Accepted Submission(s): 687


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).
 

Output
Output one line.
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


这里要考虑闰年的2月这种特殊的情况

题目中还有个bug 要注意的是 当月份或者日子 (month ==0|| day==0)  输出  illegal


#include<stdio.h>
int year[10010],month[15]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int main()
{
   year[0]=0;
   int i;
   for( i=1; i<10000 ;i++)
   {
      if( (i%4==0&&i%100!=0) || (i%400==0) ) year[i]+=year[i-1]+366; //累积年份所包含的日子
      else year[i]+=year[i-1]+365;     
      
   }
   int ye,mo,day,sum,judge;
   while( scanf("%d %d %d",&ye,&mo,&day)!=EOF )
   {
          
          if( ye<=0 || ye>=10000 || mo<=0 || mo>12 || day<=0 || day>31)  //限制条件
          {
              printf("illegal\n");
              continue;
          }
          if(mo==4||mo==6||mo==9||mo==11){   //也月份为30天的限制条件
                              if(day>30)
                               {
                                 printf("illegal\n");
                                 continue;
                               }  
                             }  
          if( (ye%4==0&&ye%100!=0) || (ye%400==0) )    //判断是否为闰年2月
          {   
                  if(mo==2 && day>29) {printf("illegal\n");continue;}      //闰年2月的限制条件
          }
          else {
                 if(mo==2 && day>28) {                                  //平年2月的限制条件
                                         printf("illegal\n");
                                         continue;
                                      }
               }
       
          sum=0;
          sum=year[ye-1]+month[mo-1]+day;    //累积日子数来警醒判定  要注意的是 1year  1month 1day 为星期六 
          if( (ye%4==0&&ye%100!=0) || (ye%400==0)  ){ if(mo>=3)sum+=1;} 
          judge=sum%7;
          switch(judge)
            {
               case 6:printf("Saturday\n");break;
               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;
               
            }
          
          
   }
    
    
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值