• Calendar


    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



    解题思路:这是一道日历题,对于这类题,我们最主要要学习的是被调函数的使用,这里写了两个被调函数,分别用于求年份和月份。
    上代码:
     1 #include<stdio.h>
     2 int year(int y)
     3 {
     4     if(y%4==0&&y%100!=0||y%400==0)
     5         return 366;
     6     else
     7         return 365;
     8 }
     9 int month(int y,int m)  
    10 {  
    11    if(y%4==0&&y%100!=0||y%400==0)
    12    {  
    13         if(m==2)  
    14            return 29;  
    15    }  
    16    else  
    17    {  
    18        if(m==2)  
    19         return 28;  
    20    }  
    21    if(m==1||m==3||m==5||m ==7||m==8||m==10||m== 12)///一三五七八十腊
    22     return 31;  
    23    else  
    24     return 30;  
    25 }  
    26 int main()
    27 {
    28     char w[7][20]={"Saturday" ,"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
    29     int n,y,m,d;
    30     while(scanf("%d",&n)!=EOF)
    31     {
    32         if(n==-1)
    33             break;
    34         y=2000,m=1,d=n%7;
    35         while(n>year(y))
    36         {
    37             n=n-year(y);
    38             y++;
    39         }
    40         while(n>=month(y,m))
    41         {
    42             n=n-month(y,m);
    43             m++;
    44         }
    45         n++;
    46         printf("%d-%02d-%02d %s
    ",y,m,n,w[d]);  
    47     }  
    48     return 0;  
    49 }  
    
    
    
    
    
  • 相关阅读:
    面向对象之继承
    面向对象
    Python—文件和内建函数 open(),file()
    如何在命令行打开文件夹?
    Anaconda在win10下的安装
    Python—for循环和range()内建函数
    python—基础练习2
    python—数据类型
    python—字符编码
    python—基础练习1
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8845970.html
Copyright © 2020-2023  润新知