zoukankan      html  css  js  c++  java
  • POJ2080:Calendar(计算日期)

    Calendar
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 12842   Accepted: 4641

    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

    注意:选取 Day of Week is Sunday 的日期作为基准日期.
    #include <iostream>
    using namespace std;
    int mds[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char Week[8][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    bool isLeap(int year)
    {
        if((year%4==0&&year%100!=0)||year%400==0)
        {
            return true;
        }
        return false;
    }
    int main()
    {
        int standard=0;
        for(int i=2000;i<=2015;i++)    
        {
            if(isLeap(i))    standard+=366;
            else    standard+=365;    
        }
        for(int i=1;i<=6;i++)
        {
            standard+=mds[i];
        }
        if(isLeap(2016))    standard++;
        standard+=3;//2016-07-03 is Sunday.
        int diff;
        int days;
        while(cin>>days&&days!=-1)
        {
            diff=days-standard;
            diff++;
            int year=2000;
            int d=1;
            int m=1;
            while(true)
            {
                if(isLeap(year))
                {
                    if(days>=366)
                    {
                        days-=366;
                        year++;
                    }
                    else    break;
                }    
                else
                {
                    if(days>=365)
                    {
                        days-=365;
                        year++;
                    }
                    else    break;
                }
            }
            while(true)
            {
                int ms=mds[m];
                if(isLeap(year)&&m==2)
                {
                    ms++;
                }
                if(days>=ms)
                {
                    days-=ms;
                    m++;
                }
                else    break;
            }
            d+=days;
            cout<<year<<"-";
            if(m<10)    cout<<"0";
            cout<<m<<"-";
            if(d<10)    cout<<"0";
            cout<<d<<" ";
            
            if(diff>=0)
            {
                diff%=7;
                cout<<Week[diff]<<endl;
            }
            else
            {
                diff=-diff;
                diff%=7;
                if(diff==0)    diff=7;
                cout<<Week[7-diff]<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    orleans 的一种模式
    在.net4的环境下使用Microsoft.AspNet.SignalR.Client 2.4.0
    微信卡券领用的附加测试
    SVN忽略本地文件不提交,同时不删除服务器上的文件
    SQL Server 2017安装错误:Polybase要求安装Oracle JRE 7更新51或更高版本的两种解决方法
    SQL Server遍历表(临时表)
    无法确定条件表达式的类型,因为“DateTime”和“<null>”之间没有隐式转换|Nullable类型问题与?:条件运算符
    C# 反射获取对象的内容
    c# 计算执行时间,性能,运行时间Stopwatch
    JS,JQuery循环数组,循环对象生成需要的数据
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5641854.html
Copyright © 2011-2022 走看看