c语言编程练习题:7-66 时间换算
作者:yunjinqi   类别:    日期:2023-05-29 21:56:04    阅读:126 次   消耗积分:0 分    

image.png

#include <stdio.h>
int main(){
    int n;
    int hour,minute,second;
    int new_hour,new_minute,new_second;
    if (scanf("%d:%d:%d",&hour,&minute,&second)!=EOF && scanf("%d",&n)!=EOF){
        //printf("%d %d %d %d",hour,minute,second,n);
        if (second+n>=60){
            new_second=second+n-60;
            new_minute=minute+1;
            if (new_minute>=60){new_hour=hour+1;new_minute=new_minute-60;}
            else{new_hour=hour;}
            }
        else{
            new_second=second+n;
            new_minute=minute;
            new_hour = hour;
        }
        //判断最新的时间是否超过了24小时
        if (new_hour>=24){new_hour=new_hour-24;}
        printf("%02d:%02d:%02d",new_hour,new_minute,new_second);
    }else{
        printf("input hour minute second n occurs wrong");
    }
    return 0;
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/191
上一篇:c语言编程练习题:7-65 字符串替换
下一篇:c语言编程练习题:7-67 What is a computer?