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;
}
时间: 2024-10-13 13:08:10

POJ2080:Calendar(计算日期)的相关文章

Calendar计算日期

一.周六三月进去.星期天 Calendar calendar=Calendar.getInstance();//当前日期 Calendar calendar2=Calendar.getInstance(); int curYear = calendar2.get(Calendar.YEAR); // 得到系统年份 int curMonth = calendar2.get(Calendar.MONTH); // 得到系统月份 int curDay=calendar2.get(Calendar.DA

sqlserver计算日期

在网上找到的一篇文章,相当不错哦O(∩_∩)O~ 这是计算一个月第一天的SQL 脚本:  SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate())-1, 0) --上个月的第一天 SELECT DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))  --当月的最后一天 SELECT DATEAD

计算日期

#include<iostream> using namespace std; int Monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int N; cin>>N; while(N--) { int year,month,day; cin>>year>>month>>day; int result=0; for(int i=0;i<=month-2;i++)

计算日期时间 自动加1天 PHP计算闰年 java与PHP时间戳对比区别

昨天写一个同步数据库的模块  从一个数据库同步到另外一个数据库,因为数据较多,不可能一次性全部搬迁过去,所以就按照每天搬迁! 写了一个 模块,点击加1,只要点击一次,自动从A数据库取出1天的数据, 并插入到B数据库, 在代码中你可能看到 计算时间的时候用到了 时间戳,并且时间戳加了好多 ‘0’ 是因为那是sqlserver 是公司java同事写的,为了PHP计算的时间戳与之匹配, 所以通过加‘0’之后  对比 时间戳取出一天的数据! 这里顺便整理一下 java和PHP之间的时间戳的区别! jav

js计算日期相差的天数

在网站开发中,经常会遇到计算日期相差的天数,js 没有提供相应的方法,所以自己写一个,方便将来查看: 代码: function DateDiff(sDate1, sDate2, splitStr) { //sDate1和sDate2是2006-12-18格式 var aDate, oDate1, oDate2, iDays; aDate = sDate1.split(splitStr); oDate1 = new Date(aDate[1] + splitStr + aDate[2] + spl

基姆拉尔森公式--计算日期周几

基姆拉尔森公式计算日期周几 #include <iostream> using namespace std; string week[] ={"Mon","tue","Wen","Thu","Fri","Sat","Sun"}; int main(){ int y,m,d; while(cin>>y>>m>>d){ if

计算日期函数

[email protected][email protected]=GETDATE() [email protected][email protected]=3 --1.指定日期该年的第一天或最后一天--A. 年的第一天SELECTCONVERT(char(5),@dt,120)+'1-1' --B. 年的最后一天SELECTCONVERT(char(5),@dt,120)+'12-31' --2.指定日期所在季度的第一天或最后一天--A. 季度的第一天SELECTCONVERT(dateti

C语言计算日期间隔天数的经典算法解析

在网上看到了一个计算日期间隔的方法,咋一看很高深,仔细看更高神,很巧妙. 先直接代码吧 #include <stdio.h> #include <stdlib.h> int day_diff(int year_start, int month_start, int day_start , int year_end, int month_end, int day_end) { int y2, m2, d2; int y1, m1, d1; m1 = (month_start + 9)

[时间序列处理]python中计算日期差

参考自:http://blog.csdn.net/sinat_37487842/article/details/72864462 1 import time 2 import datetime 3 4 #计算两个日期相差天数,自定义函数名,和两个日期的变量名. 5 def Caltime(date1,date2): 6 #%Y-%m-%d为日期格式,其中的-可以用其他代替或者不写,但是要统一,同理后面的时分秒也一样:可以只计算日期,不计算时间. 7 #date1=time.strptime(da