输入任意时间,输出该时刻的下一秒--腾讯笔试

#include <stdio.h>
#include <stdlib.h>
/*
问题描述:
输入任意时间,输出改时刻的下一秒:


例如输入2008年2月28日23时59分59秒,
输出为:2008年2月29日0时0分0秒。
输入一个时间,输出下一秒的时间。
例如输入2008年2月28日23时59分59秒,
输出为:2008年2月29日0时0分0秒。
*/
int isleapyear(int year);//闰年判断函数
//普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
//世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);
int main()
{
    int year, month, day, hour, minute, second;
    char s1[10], s2[10], s3[10], s4[10], s5[10], s6[10];
    while(scanf("%d%2s%d%2s%d%2s%d%2s%d%2s%d%2s", &year, s1, &month, s2, &day, s3, &hour, s4, &minute, s5, &second, s6) != EOF)
    //用char型数组来接收中文字符,在C语言中一个中文字符2个字节
    {
        printf("%d%s%d%s%d%s%d%s%d%s%d%s\n", year, s1, month, s2, day, s3, hour, s4, minute, s5, second, s6);
        if(second == 59)
        {
            second = 0;
            if(minute == 59)
            {
                minute = 0;
                if(hour == 23)
                {
                    hour = 0;
                    switch(month)
                    {
                    case 2:
                        {
                            if(isleapyear(year))
                            {
                                if(day == 29)
                                {
                                    day =1;
                                    month++;
                                }
                                else
                                {
                                    day++;
                                }
                            }
                            else
                            {
                                if(day == 28)
                                {
                                    day = 1;
                                    month++;
                                }
                            }
                            break;
                        }
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        {
                            if(day == 31)
                            {
                                day = 1;
                                if(month == 12)
                                {
                                    month = 1;
                                    year++;
                                }
                                else month++;
                            }
                            else day++;
                            break;
                        }
                    default://(4、6、9、11)
                        {
                            if(day == 30)
                            {
                                day = 1;
                                month++;
                            }
                            else day++;
                            break;
                        }
                    }
                }
                else hour++;
            }
            else minute++;
        }
        else second++;
        printf("%d%s%d%s%d%s%d%s%d%s%d%s\n", year, s1, month, s2, day, s3, hour, s4, minute, s5, second, s6);
    }
    return 0;
}
int isleapyear(int year)//是否闰年判断函数,为闰年返回1,否则返回0
{
    if(year%100)//不能被100整除,普通年
    {
        if(year%4) return 0;//不能被4整除,不是闰年
        else return 1;
    }
    else //被100整除,世纪年
    {
        if(year%400) return 0;//不能被400整除,不是闰年
        else return 1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值