北邮oj bupt oj 日期

日期

时间限制 1000 ms 内存限制 65536 KB

题目描述

请你计算出第X年Y月Z日是第X年的第几天。其中,1月1日是第一天,1月2日是第二天,以此类推。

计算时请注意闰年的影响。对于非整百年,年数能整除4是闰年,否则不是闰年;对于整百年,年数能整除400是闰年,否则不是闰年。如1900年和1901年不是闰年,而2000年和2004年是闰年。

输入格式

第一行有一个整数T (T≤100),表示一共有T组数据需要你处理。
接下来一共有T行,每行是一个如下格式的字符串:X:Y:Z,表示你需要计算第X年Y月Z日是第X年的第几天。其中X是一个大于0,小于2100的整数。保证字符串的格式都是合法的,字符串所表示的日期也都是存在的。

输出格式

对于每组数据,你需要输出一个整数,表示所求得的结果。

输入样例

2
2013:4:12
112:4:12

输出样例

102
103

AC代码

#include <bits/stdc++.h>
#define ISYEAP(x) x%100!=0&&x%4==0||x%400==0?1:0
using namespace std;
int dayOfMonth[13][2]={
    0,0,
    31,31,
    28,29,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31
};
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int ans=0;
        int year,mon,day;
        scanf("%d:%d:%d",&year,&mon,&day);
        int i,j;
        if(ISYEAP(year)){
            j=1;
        }else{
            j=0;
        }
        for(i=1;i<mon;i++){
            ans+=dayOfMonth[i][j];
        }
        ans+=day;
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/88357800