Calendar-poj-2080

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

 

测试数据:

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
 

解题思路:

1、  先判断输入的数据是否大于365

2、  再判断月份的天数。

3、  对7取余,算出星期天即可。

注意事项:闰年的二月,和题目要求输入数据0 是2000-01-01.

程序代码:

#include<stdio.h>

#include<string.h>

int main()

{

    int runnian (int year);

    int tian(int year, int mo);

    int n,m,j,i,s,k,year,mo,day;

    char a[8][20];

    while(scanf("%d",&day)&&day!=-1)

    {

      n=day+1;                      //数据要求刚好多一天。

      year=2000;

      while(n>365)

       {

        k=runnian(year);            //若是闰年则减去366 若不是闰年 则减去365

        if(n>k)

        n=n-k;

        else

        break;

        year=year+1;

       }

       mo=1;

       while(n>=28)

        {

          j=tian(year,mo);                    //调用函数 判断月数 是多少天的

          if(n>j)

           n=n-j;

           else

           break;

          mo+=1;

        }

        s=(day+6)%7;

        strcpy(a[0],"Sunday");              // 把星期几个英文赋值到数组里保存

        strcpy(a[1],"Monday");

        strcpy(a[2],"Tuesday");

        strcpy(a[3],"Wednesday");

        strcpy(a[4],"Thursday");

        strcpy(a[5],"Friday");

        strcpy(a[6],"Saturday");

        printf("%d-%02d-%02d %s ",year,mo,n,a[s]);

        /*switch(s)                                   //判断对七取余后是什么。  2000-1-1是星期六

        {

         case 0: printf(" ");break;

         case 1: printf(" ");break;

         case 2: printf(" ");break;

         case 3: printf(" ");break;

         case 4: printf(" ");break;

         case 5: printf(" ");break;

         case 6: printf(" ");break;

        }*/

    }

}

int runnian (int year)

{

    if(year%400==0||(year%4==0&&year%100!=0))

      return 366;

    else

      return 365;

}

int tian(int year,int mo)

{

    if(mo==1||mo==3||mo==5||mo==7||mo==8||mo==10||mo==12)

    return 31;

    if(mo==4||mo==6||mo==9||mo==11)

    return 30;

    if(mo==2)

     {

     if(runnian(year)==366)

      return 29;

      else

       return 28;

      }

}

原文地址:https://www.cnblogs.com/zhouhongweihpu/p/3228304.html