HDU 2005 第几天?(闰年判断)

传送门:

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): 183321    Accepted Submission(s): 65026


Problem Description
给定一个日期,输出这个日期是该年的第几天。
 
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 
Sample Input
1985/1/20 2006/3/12
 
Sample Output
20 71
 
Author
lcy
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  2008 2009 2014 2011 2017 
 
分析:
闰年有366天,二月份比普通年多一天,右29天
普通年:
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
闰年判断:
 
1.%4==0且%100!=0
2.%400==0
两个条件满足一个就可以
code:
#include<bits/stdc++.h>
using namespace std;
int f(int y,int m,int d)
{
    int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    if((y%4==0&&y%100!=0)||y%400==0)
    {
        a[1]=29;
    }
    int sum=0;
    for(int i=0;i<=m-2;i++)
    {
        sum+=a[i];
    }
    sum+=d;
    return sum;
}
int main()
{
    int y,m,d;
    while(~scanf("%d/%d/%d",&y,&m,&d))
    {
        printf("%d
",f(y,m,d));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yinbiao/p/9370282.html