HDU2005 第几天?【日期计算】

第几天?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 150077    Accepted Submission(s): 53872
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20 2006/3/12
Sample Output
20 71
Author
lcy
Source

问题链接HDU2005 第几天?

问题简述:有多个测试实例,输入一个日期,算出是那一年的第几天。

问题分析:(略)

程序说明:这里给出的程序,与一般教科书的做法略有不同,程序逻辑要简洁一些。


AC的C语言程序如下:

/* HDU2005 第几天? */

#include <stdio.h>

int leapyear_day(int year, int month)
{
    // 1月或2月不用加1天,其他月份润年加1天,非润年不用加1天
    if(month <= 2)
        return 0;
    else
        return ( ((year%4==0) && (year%100!=0)) || (year%400==0) )?1:0;
}

int main(void)
{
    int year, month, day;
    int days;
    int monthsum[] = {  // 到某月份的天数,润年另外加天数
          0                                 // 1月
        , 31                                // 2月
        , 31+28                             // 3月
        , 31+28+31                          // 4月
        , 31+28+31+30                       // 5月
        , 31+28+31+30+31                    // 6月
        , 31+28+31+30+31+30                 // 7月
        , 31+28+31+30+31+30+31              // 8月
        , 31+28+31+30+31+30+31+31           // 9月
        , 31+28+31+30+31+30+31+31+30        // 10月
        , 31+28+31+30+31+30+31+31+30+31     // 11月
        , 31+28+31+30+31+30+31+31+30+31+30  // 12月
    };

    while(scanf("%d/%d/%d", &year, &month, &day) != EOF) {
        // 天数 = 润年需要加的天数(根据年和月计算) + 到某月天数 + 月内天数
        days = leapyear_day(year, month) + monthsum[month-1] + day;

        // 输出结果
        printf("%d
", days);
    }

    return 0;
}


原文地址:https://www.cnblogs.com/tigerisland/p/7564693.html