UVA893 Y3K Problem【日期】

We have heard a lot recently about the Y2K problem. According to the doom sayers, planes will fall out of the skies, businesses will crash and the world will enter a major depression as the bugs in software and hardware bite hard. While this panic is a very satisfactory state of affairs for the computing profession, since it is leading to lots of lucrative jobs, it will have a tendency to bring the profession into disrepute when almost no problems occur on 1/1/00. To help avoid this unseemly behaviour on any future occasion, you must write a program which will give the correct date for (almost) any number of future days - in particular, it must correctly predict the date N days ahead of any given date, where N is a number less than 1,000,000,000 and the given date is any date before the year 3000.
    Remember that in the standard Gregorian calendar we use there are 365 days in a year, except for leap years in which there are 366. Leap years are all years divisible by 4 and not divisible by 100, except for those divisible by 400. Thus 1900 was not a leap year, 1904, 1908 … 1996 were leap years, 2000 will be a leap year, 2100 will not be a leap year, etc. The number of days in each month in a normal year is 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; in a leap year, the second month has 29 days.
Input
Input will consist of lines containing four numbers, separated by one or more spaces. The first number on each line will be the number of days you have to predict (between 0 and 999999999), followed by the date in the format DD MM Y Y Y Y where DD is the day of the month (1 to 31), MM is the month (1 to 12), and Y Y Y Y is the year (1998 to 2999), all inclusive. The input will be terminated by a line containing four 0’s.
Output
For each line of input, one output line should be produced. This line should contain the date which is the required number of days ahead of the input date, written in the same format as the input dates.
Sample Input
1 31 12 2999
40 1 2 2004
60 31 12 1999
60 31 12 2999
146097 31 12 1999
999999 1 1 2000
0 0 0 0
Sample Output
1 1 3000
12 3 2004
29 2 2000
1 3 3000
31 12 2399
27 11 4737

问题链接UVA893 Y3K Problem
问题简述:(略)
问题分析
    日期计算问题。给定天数x和日期,求那个日期开始过x天后的日期?
    日期问题通常用模拟法进行计算。
程序说明:(略)
参考链接:(略)
题记:多数情况下,找不到直接计算的公式就模拟。

AC的C++语言程序如下:

/* UVA893 Y3K Problem */

#include <bits/stdc++.h>

using namespace std;

const int YDAYS = 365;
const int MDAYS[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// 闰年计算函数
int leapyear(int year) {
    return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 1 : 0;
}

int howdays(int y, int m, int d)
{
    int ans = d;
    for(int i = 1; i < m; i++) {
        ans += MDAYS[i];
        if(i == 2) ans += leapyear(y);
    }
    return ans - 1;
}

int main()
{
    int days, d, m, y;

    while (~scanf("%d%d%d%d",&days, &d, &m, &y) && (days || d || m || y)) {
        days += howdays(y, m, d);
        m = 1;
        // 一年一年过
        for(;;) {
            int ydays = YDAYS + leapyear(y);
            if(days >= ydays) days -= ydays, y++;
            else break;
        }
        // 一个月一个月过
        for(;;) {
            int mdays = MDAYS[m];
            if(m == 2) mdays += leapyear(y);
            if(days >= mdays) days -= mdays, m++;
            else break;
        }
        // 日
        d = days + 1;

        printf("%d %d %d\n", d, m, y);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值