hdu 2005 求第几天

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2005

                                        第几天?

                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                   Total Submission(s): 177873    Accepted Submission(s): 63023


Problem Description
给定一个日期,输出这个日期是该年的第几天。
 
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 
Sample Input
1985/1/20
2006/3/12
 
Sample Output
20
71
 
#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\n", days);
    }

    return 0;
}
 
2018-04-23

猜你喜欢

转载自www.cnblogs.com/00isok/p/8922779.html