poj 2080 Calendar

Calendar
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9885   Accepted: 3705

Description

A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years and centuries. The terms of hour, day, month, year and century are all units of time measurements of a calender system.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week.

Input

The input consists of lines each containing a positive integer, which is the number of days that have elapsed since January 1, 2000 A.D. The last line contains an integer −1, which should not be processed.
You may assume that the resulting date won’t be after the year 9999.

Output

For each test case, output one line containing the date and the day of the week in the format of "YYYY-MM-DD DayOfWeek", where "DayOfWeek" must be one of "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".

Sample Input

1730
1740
1750
1751
-1

Sample Output

2004-09-26 Sunday
2004-10-06 Wednesday
2004-10-16 Saturday
2004-10-17 Sunday

Source

 
由于数据不具代表性,再给一些测试数据
输入    输出
59 2000-02-29 Tuesday
60 2000-03-01 Wednesday
365 2000-12-31 Sunday
366 2001-01-01 Monday

1460 2003-12-31 Wednesday
1461 2004-01-01 Thursday
1520 2004-02-29 Sunday
1521 2004-03-01 Monday
1826 2004-12-31 Friday
1827 2005-01-01 Saturday

36524 2099-12-31 Thursday
36525 2100-01-01 Friday
36583 2100-02-28 Sunday
36584 2100-03-01 Monday
36889 2100-12-31 Friday
36890 2101-01-01 Saturday
 
146096 2399-12-31 Friday
146097 2400-01-01 Saturday
146156 2400-02-29 Tuesday
146157 2400-03-01 Wednesday

思路分析:我是先找年分,再找月,最后找天。

找年时,我是让你年从2000开始,判断是不是闰年,是就让变量加366,如果不是就让储存变量加365,判断与输入天数的关系,>=的话跳出循环,输出年份,

用类似的方法可求出月份,

这个题的关键是要剩余天数与所求天数的比较,如果剩余的天数大于这年这个月份的天数,那么月数要加1,更为特殊的是如果月份是12 ,那加完1,后是13 那么上面所求年的年份也要更新

总之一句话,思路很简单,就是输出时候,要好好思考一番


没有整理的代码:
#include "stdio.h"
int main()
{int i,j,k,n,sum,m=0;
int q=0,r=0;
int x,y;
int z;
int month;
  while(scanf("%d",&n),n!=-1)
   {  sum=0;
       for(i=2000;;i++)
      {       m=sum;
              if(i%400==0 ||(i%4==0 && i%100!=0))
              sum+=366;
              else
              sum+=365;
              if(sum>n)
               {printf("%04d-",i);
              // printf("%d ",sum);
              // printf("%d",m);
               break;
               }
               if(sum==n)
               {printf("%04d-",i+1);
               break;
               }
              
       }
       if(sum>n)
       j=i;
       else if(sum==n)
         j=i+1;
       if(sum==n)
        k=n-sum;
        else
        k=n-m;
      //printf("%d",j);
      q=0;
  
       for(i=1;i<=13;i++)
         {  r=q;
            if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
            q+=31;
            if(i==4 || i==6 || i==9 || i==11)
            q+=30;
            if(i==2)
            if(j%400==0 ||(j%4==0 && j%100!=0))
            q+=29;
            else
            q+=28;
            if(q>k)
             { /*  z=i;
              switch(z)
              {case 1:  y=31;break;
               case 2:  if(j%400==0 ||(j%4==0 && j%100!=0))
                           y=29;
                           else
                           y=28;break;
               case 3:  y=31;break;
               case 4: y=30;break;
               case 5: y=31;break;
               case 6:  y=30;break;
               case 7:  y=31;break;
               case 8:  y=31;break;
               case 9:  y=30;break;
               case 10:  y=31;break;
               case 11:  y=30;break;
               case 12:  y=31;break;
               }*/
              if(i<=12)
              {   if((k-r)>y)
                {printf("%02d-",i+1);
                printf("%02d ",k-r+1-y);
                break;
                }
                else
                 { printf("%02d-",i);
                  printf("%02d ",k-r+1);}
                 break;
                 }
                 }
               if(i==13)
               {
                 if((k-r)>y)
                {printf("%02d-",i-11);
                printf("%02d ",k-r+1-y);
                 break;
                 }
                 else
                {printf("%02d-",i-12);
                printf("%02d ",k-r+1);
                break;
                }
               // printf("%d  ",i);
               // break;
               }
              }
           
            /*if(q>=k)
             {  z=i;
              switch(z)
              {case 1:  y=31;break;
               case 2:  if(j%400==0 ||(j%4==0 && j%100!=0))
                           y=29;
                           else
                           y=28;break;
               case 3:  y=31;break;
               case 4: y=30;break;
               case 5: y=31;break;
               case 6:  y=30;break;
               case 7:  y=31;break;
               case 8:  y=31;break;
               case 9:  y=30;break;
               case 10:  y=31;break;
               case 11:  y=30;break;
               case 12:  y=31;break;
               } 
               if((k-r+1)<=y)
              {printf("%02d-",i);
              printf("%02d ",k-r+1);
                break;
               }
             else
              {if((i+1)>12)
              {printf("%02d-",i-11);
              printf("%02d ",k-r-y+2);
              break;
              }
              else
               {printf("%02d-",i+1);
               printf("%02d ",k-r-y+2);
               break;
               }
              }
            
             }
           }
          
           //printf("%02d ",k-r+1);
           */
           switch(n%7)
           {case 0: printf("Saturday "); break;
           case 1: printf("Sunday "); break;
           case 2: printf("Monday "); break;
           case 3: printf("Tuesday "); break;
           case 4: printf("Wednesday "); break;
           case 5: printf("Thursday "); break;
           case 6: printf("Friday "); break;
           }
       }
       return 0;
      
      }
 
 
整理后的代码:
#include "stdio.h"
int main()
{int i,j,k,n,sum,m=0;
int q=0,r=0;
int x,y;
int z;
int month;
  while(scanf("%d",&n),n!=-1)
   {  sum=0;
       for(i=2000;;i++)
      {       m=sum;
              if(i%400==0 ||(i%4==0 && i%100!=0))
              sum+=366;                     //找年份
              else
              sum+=365;
              if(sum>n)
               {printf("%04d-",i);
              // printf("%d ",sum);
              // printf("%d",m);
               break;
               }
               if(sum==n)
               {printf("%04d-",i+1);
               break;
               }
              
       }
       if(sum>n)
       j=i;
       else if(sum==n)
         j=i+1;
       if(sum==n)
        k=n-sum;
        else
        k=n-m;
      //printf("%d",j);
      q=0;
  
       for(i=1;i<=13;i++)                                   //找月份和天
         {  r=q;
            if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
            q+=31;
            if(i==4 || i==6 || i==9 || i==11)
            q+=30;
            if(i==2)
            if(j%400==0 ||(j%4==0 && j%100!=0))
            q+=29;
            else
            q+=28;
            if(q>k)
             {
              if(i<=12)
              {   if((k-r)>y)
                {printf("%02d-",i+1);
                printf("%02d ",k-r+1-y);
                break;
                }
                else
                 { printf("%02d-",i);
                  printf("%02d ",k-r+1);}
                 break;
                 }
                 }
               if(i==13)
               {
                 if((k-r)>y)
                {printf("%02d-",i-11);
                printf("%02d ",k-r+1-y);
                 break;
                 }
                 else
                {printf("%02d-",i-12);
                printf("%02d ",k-r+1);
                break;
                }
             
               }
              }
           
          
           switch(n%7)        //输出星期几      只需对7取余即可
           {case 0: printf("Saturday "); break;
           case 1: printf("Sunday "); break;
           case 2: printf("Monday "); break;
           case 3: printf("Tuesday "); break;
           case 4: printf("Wednesday "); break;
           case 5: printf("Thursday "); break;
           case 6: printf("Friday "); break;
           }
       }
       return 0;
      
      }
     
原文地址:https://www.cnblogs.com/songmingtao/p/3228314.html