HOJ 2315 Time(模拟)

Description

Kim是一个掌控时间的大师。不同于一般人,他习惯使用秒来计算时间。如果你问他现在是几点,他会告诉你现在是今天的xxxx秒。Mik想要考考Kim。他想知道从某一天的00:00:00开始,经过s秒后是哪一天。但是Mik不会计算答案,他需要你的帮助。

注意:我们认为一天从00:00:00开始,到23:59:59结束。00:00:00经过1秒后是00:00:01;从00:00:00开始,加86400(60*60*24)秒后就是下一天的00:00:00.

Input

第一行一个整数T表示数据组数。

接下来T行,每行一个日期yyyy-MM-dd,接下来一个整数s表示s秒。

Output

对于每个输入,输出一行yyyy-MM-dd 表示答案。对于不足两位的数要补齐前导0。

Sample Input
3
2016-12-10 1000
2016-02-28 86400
2016-01-01 1000000
Sample Output
2016-12-10
2016-02-29
2016-01-12
Hint

T<=100

s<=2147483647

日期在1800-01-01到2100-01-01之间

 

闰年的判断:

1.能被4整除且不能被100整除的为闰年.

2.能被400整除的是闰年.

 

Source
"科林明伦杯"哈尔滨理工大学第六届程序设计团队赛
#include<cstdio>
#include<iostream>

using namespace std;

char s[10];
int table[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int t,year,mon,day;
long long ti;

int check(int n)
{
    if(n%4==0)return 1;
    if(n%4==0&n%100!=0)return 1;
    return 0;
}

int getday(int n)
{
    if(n==2)return check(year)?29:28;
    return table[n];
}

int main()
{
    cin>>t;
    while(t--)
    {
        year=mon=day=0;
        cin>>s>>ti;

        year+=(s[0]-'0')*1000;
        year+=(s[1]-'0')*100;
        year+=(s[2]-'0')*10;
        year+=(s[3]-'0');
        mon+=(s[5]-'0')*10;
        mon+=s[6]-'0';
        day+=(s[8]-'0')*10;
        day+=s[9]-'0';

        day+=(ti/86400);

        while(day>0)
        {
            day-=getday(mon++);
            if(day<=0){mon--;day+=getday(mon);break;}
            if(mon==13)
            {
                mon=1;
                year++;
            }
        }
        printf("%d-",year);
        if(mon<10)printf("0%d-",mon);else printf("%d-",mon);
        if(day<10)printf("0%d\n",day);else printf("%d\n",day);
    }
    return 0;
}

 

posted @ 2017-12-13 17:37  wa小怪兽  阅读(153)  评论(0编辑  收藏  举报