POJ 2964:日历问题 日期转换+闰年月份可放在一个month[2][12]数组里

2964:日历问题

  • 查看

  • 提交

  • 统计

  • 提示

  • 提问

  • 总时间限制:

    1000ms

  • 内存限制:

    65536kB

  • 描述

    在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。 给定从公元2000年1月1日开始逝去的天数,你的任务是给出这一天是哪年哪月哪日星期几。

  • 输入

    输入包含若干行,每行包含一个正整数,表示从2000年1月1日开始逝去的天数。输入最后一行是−1, 不必处理。可以假设结果的年份不会超过9999。

  • 输出

    对每个测试样例,输出一行,该行包含对应的日期和星期几。格式为“YYYY-MM-DD DayOfWeek”, 其中 “DayOfWeek” 必须是下面中的一个: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" 或 "Saturday“。

  • 样例输入

    1730 1740 1750 1751 -1

  • 样例输出

    2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday

  • 提示

    2000.1.1. 是星期六

    思路

    这种题经常出,整理下来比较好

    • int month[2][12],注意是先放满第二维,再开始放第一维,所以先放非闰年的,再放闰年的

    • if(year%4!=0||(year%100==0&&year%400!=0))return 0;
      else return 1; 经典写法,不要写的太复杂

    • 然后天数逐年减去,直到减不动再逐月减,直到减不动即可。

    • 如果求2000年a月b日到2004年c月d日差的天数,可先求2000 2001 2002 2003加起来,然后再加上c月d日减去a月d日即可

    #include <iostream>
    #include<bits/stdc++.h>
    #define each(a,b,c) for(int a=b;a<=c;a++)
    #define de(x) cout<<#x<<" "<<(x)<<endl
    using namespace std;
    
    const int maxn=500+5;
    const int inf=0x3f3f3f3f;
    
    /*
    1730
    1740
    1750
    1751
    -1
    */
    char dayofweeek[7][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"};
    int year[2]={365,366};
    int month[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31};
    int judge(int year)
    {
        if(year%4!=0||(year%100==0&&year%400!=0))return 0;
        else return 1;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n!=-1)
        {
            int weekid=(n+6)%7;
            int ans1=2000;
            while(n>=year[judge(ans1)])
            {
                n-=year[judge(ans1)];
               // de(year[judge(ans1)]);
                ans1++;
            }
            int ans2=0;
            //de(n);
            while(n>=month[judge(ans1)][ans2])
            {
                //de(ans2);
                //de(month[judge(ans1)][ans2]);
                n-=month[judge(ans1)][ans2];
                ans2++;
                if(ans2==12)ans2=0;
            }
            int ans3=n;
            printf("%d-%02d-%02d %s
    ",ans1,ans2+1,ans3+1,dayofweeek[weekid]);
        }
    
        return 0;
    }
    
    
    
原文地址:https://www.cnblogs.com/Tony100K/p/12256485.html