poj 2080 Calendar

Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I64u

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

求2000-01-01日之后的第n天是星期几,并把日期输出;
1:先计算出n天后的日期(暴力模拟)
2:计算出日期后,代入公式求解星期几:
公式:w=[c/4]-2c+y+[y/4]+[26(m+1)/10]+d-1
w为所求日期的星期数,如果求得的数大于(小于)7,就减去(加上)7的倍数,直到余数小于7为止。C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值。),
[]表示对括号内数字取整。特别注意:如果月份是1、2月,则月数为13、14,也就是月的范围是3-14。 

代码:
#include <iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int year,month,day;
int leap_mon[12]= {31,29,31,30,31,30,31,31,30,31,30,31};
int mon[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
bool leap(int year)
{
    if(year%400==0||(year%4==0&&year%100!=0))
        return true;
    return false;
}
void c(int n)//模拟求解日期
{
    int i,j;
    for(i=2000; n; i++)
    {
        if(leap(i))
        {
            if(n>=366)
            {
                n-=366;
                year++;
                continue;
            }
            else
            {
                for(j=0; j<12; j++)
                {
                    if(n>=leap_mon[j])
                    {
                        n-=leap_mon[j];
                        month++;
                        if(month>12)
                        {
                            year++;
                            month-=12;
                        }
                        continue;
                    }
                    else
                    {
                        day+=n;
                        if(day>leap_mon[month-1])
                            month++;
                        if(month>12)
                        {
                            year++;
                            month-=12;
                        }
                        n=0;
                        break;
                    }
                }
            }
        }
        else
        {
            if(n>=365)
            {
                n-=365;
                year++;
                continue;
            }
            else
            {
                for(j=0; j<12; j++)
                {
                    if(n>=mon[j])
                    {
                        n-=mon[j];
                        month++;
                        if(month>12)
                        {
                            year++;
                            month-=12;
                        }
                        continue;
                    }
                    else
                    {
                        day+=n;
                        if(day>mon[month-1])
                            month++;
                        if(month>12)
                        {
                            year++;
                            month-=12;
                        }
                        n=0;
                        break;
                    }
                }
            }
        }
    }
}
int week()//公式计算星期几
{
    int c=year/100,y=year%100,d=day,w,m;
    if(month==1||month==2)
    {
        m=month+12;
        y=(year-1)%100;
        c=(year-1)/100;
    }
    else
        m=month;
    w=floor(c/4.0)-2*c+y+floor(y/4.0)+floor(26*(m+1)/10.0)+d-1;
    if(w>=7)
        w=w%7;
    else if(w<0)
        w=(w%7+7)%7;
   // cout<<w<<endl;
    return w;
}
int main()
{
    int n;
    while(cin>>n&&n!=-1)
    {
        year=2000;
        month=1;
        day=1;
        c(n);
        switch(week())//输出
        {
        case 0:
            printf("%d-%02d-%02d Sunday\n",year,month,day);
            break;
        case 1:
            printf("%d-%02d-%02d Monday\n",year,month,day);
            break;
        case 2:
            printf("%d-%02d-%02d Tuesday\n",year,month,day);
            break;
        case 3:
            printf("%d-%02d-%02d Wednesday\n",year,month,day);
            break;
        case 4:
            printf("%d-%02d-%02d Thursday\n",year,month,day);
            break;
        case 5:
            printf("%d-%02d-%02d Friday\n",year,month,day);
            break;
        case 6:
            printf("%d-%02d-%02d Saturday\n",year,month,day);
            break;
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值