POJ calendar

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9858   Accepted: 3696

Descriptionspa

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.

Inputorm

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.

Outputip

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 Inputci

1730
1740
1750
1751
-1

Sample Outputrem

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

個人代碼:get

#include<stdio.h>
int main()
{
 int m;
 while(scanf("%ld",&m)&&m!=-1)
 {
  int n,year=2000,month=1,date=1;
  n=m%7;
  while(m>=365)
  { 
   if(leap(year)==1&&m>365)
   {
    m-=366;
    year++;
   }
   else if(leap(year)==0)
   {
    m-=365;
    year++;
   }
   else
    break;
  }
  if(leap(year)==1)
  {
   if(month==1&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==2&&m>28)
   {
    m-=29;
    month++;
   }
   if(month==3&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==4&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==5&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==6&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==7&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==8&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==9&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==10&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==11&&m>29)
   {
    m-=30;
    month++;
   }
   date=m+1;
  }
  if(leap(year)==0)
  {
   if(month==1&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==2&&m>27)
   {
    m-=28;
    month++;
   }
   if(month==3&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==4&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==5&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==6&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==7&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==8&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==9&&m>29)
   {
    m-=30;
    month++;
   }
   if(month==10&&m>30)
   {
    m-=31;
    month++;
   }
   if(month==11&&m>29)
   {
    m-=30;
    month++;
   }
   date=m+1;
  }
  if(month<10&&date<10)
   printf("%d-0%d-0%d ",year,month,date);
  else if(month<10)
   printf("%d-0%d-%d ",year,month,date);
  else if(date<10)
   printf("%d-%d-0%d ",year,month,date);
  else
   printf("%d-%d-%d ",year,month,date);
  switch(n)
  {
   case 1:printf("Sunday\n");break;
   case 2:printf("Monday\n");break;
   case 3:printf("Tuesday\n");break;
   case 4:printf("Wednesday\n");break;
   case 5:printf("Thursday\n");break;
   case 6:printf("Friday\n");break;
   default:printf("Saturday\n");
  }
 }
 return 0;
}
int leap(int y)
{
 if(y%400==0||y%100!=0&&y%4==0)
  return 1;
 else
  return 0;
}input

有點小麻煩,我先判斷年份,在作判斷年份時,我以前忘記編寫break語句,致使結果一直不對。後面對小於365或小於366的數據進行處理。我是逐月減去天數,最後獲得一個m值而後判斷日期。我同窗的代碼以下:string

#include<string.h>
#include<stdio.h>
int main()
{
 int y,m,d,n,a[13],year[10000],i,c,w;
 a[1]=31;a[3]=31;a[4]=30;a[5]=31;a[6]=30;a[7]=31;a[8]=31;a[9]=30;a[10]=31;a[11]=30;a[12]=31;
  for(i=0;i<10000;i++){
   if(((i+2000)%4==0&&(i+2000)%100!=0)||(i+2000)%400==0)
       year[i]=366;
   else
    year[i]=365;}
 while(scanf("%d",&n)&&n!=-1)
 {
  n+=1;
  for(y=2000,i=0;n>year[i];i++)
  {n-=year[i];y+=1;}
  if((y%4==0&&y%100!=0)||y%400==0)
   a[2]=29;
  else
   a[2]=28;
  for(i=1,m=1;n>a[i];i++)
  {
   n-=a[i];
   m+=1;
  }
  printf("%d-",y);
  if(m>=10)
   printf("%d-",m);
  if(m<10)
   printf("0%d-",m);
  if(n>=10)
   printf("%d ",n);
  if(n<10)
   printf("0%d ",n);
  if(m<3)
  {m+=12;y-=1;}
  d=n;
  c=y/100;
  y=y%100;
  w=((c/4)-2*c+y+(y/4)+(13*(m+1)/5)+d-1)%7;
  while(w<0)
   w+=7;
  switch(w)
  {
  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;}
  }
 }
 return 0;
}it

比個人簡單多了。io

另外一個網上的:

#include<stdio.h>  
int main(){  
    char w[7][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ,"Saturday"};  
    int m[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,  
                  0,31,29,31,30,31,30,31,31,30,31,30,31};  
    int yd[2]={365,366};  
    long day;  
    int year,month,week;  
    int i,j,flag;  
    while(scanf("%ld",&day)&&-1!=day){  
        week=(day+6)%7;//獲得星期幾   
        year=2000;  
        flag=(0==year%4&&year%100!=0)||0==year%400;//flag=1爲閏年   
        ++day;//題目說通過多少天,因此在這裏先加1   
        for(;day>yd[flag];){//獲得年份、剩餘天數   
            day-=yd[flag];   
            year++;  
            flag=(0==year%4&&year%100!=0)||0==year%400;  
        }  
        for(month=1;day>m[flag][month];++month){//獲得月份和對應天數   
            day-=m[flag][month];  
        }  
        printf("%d-%02d-%02d %sn",year,month,day,w[week]);//%02d很方便          
    }  
}
相關文章
相關標籤/搜索