zoukankan      html  css  js  c++  java
  • 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 }  
    
    
    
    
    
  • 相关阅读:
    做事要趁早
    软件企业利润率知多少
    项目管理经验谈之意外事件处理
    App中调用iPhone的home + 电源键截屏功能
    WCF学习(一)
    10月博客学习一览
    设计模式之工厂模式读后感
    请教各位大鸟(关于附件上传)
    刚来的兄弟,一起扬帆起航吧
    巧用asp导出csv格式excel报表
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8845970.html
Copyright © 2011-2022 走看看