• POJ calendar


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

    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

    我的代码:

    #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 ");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;
       default:printf("Saturday ");
      }
     }
     return 0;
    }
    int leap(int y)
    {
     if(y%400==0||y%100!=0&&y%4==0)
      return 1;
     else
      return 0;
    }

    有点小麻烦,我先判断年份,在做判断年份时,我之前忘记编写break语句,导致结果一直不对。后面对小于365或小于366的数据进行处理。我是逐月减去天数,最后得到一个m值然后判断日期。我同学的代码如下:

    #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 ");break;}
      case 1:{printf("Monday ");break;}
      case 2:{printf("Tuesday ");break;}
      case 3:{printf("Wednesday ");break;}
      case 4:{printf("Thursday ");break;}
      case 5:{printf("Friday ");break;}
      case 6:{printf("Saturday ");break;}
      }
     }
     return 0;
    }

    比我的简单多了。

    另一个网上的:

    #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很方便          
        }  
    }
  • 相关阅读:
    MOSS中的User的Title, LoginName, DisplayName, SID之间的关系
    如何在Network Monitor中高亮间隔时间过长的帧?
    SharePoint服务器如果需要安装杀毒软件, 需要注意什么?
    如何查看SQL Profiler? 如何查看SQL死锁?
    什么是Telnet
    The name or security ID (SID) of the domain specified is inconsistent with the trust information for that domain.
    Windows SharePoint Service 3.0的某个Web Application无搜索结果
    网络连接不上, 有TCP错误, 如果操作系统是Windows Server 2003, 请尝试一下这里
    在WinDBG中查看内存的命令
    The virtual machine could not be started because the hypervisor is not running
  • 原文地址:https://www.cnblogs.com/wangyouxuan/p/3228119.html
Copyright © 2020-2023  润新知