当前位置:网站首页>Exercise 9-1 time conversion (15 points)

Exercise 9-1 time conversion (15 points)

2022-06-11 22:25:00 Xiaoyan y

This question requires the preparation of procedures , With hh:mm:ss Output in the format of... After a given time n Time value after seconds ( exceed 23:59:59 From 0 The clock starts at ).

Input format :

Enter in the first line with hh:mm:ss The format of gives the start time , The second line gives the number of whole seconds n(<60).

Output format :

The output is given on one line hh:mm:ss Result time in format .

sample input :

11:59:40
30

sample output :

12:00:10

#include<stdio.h>

struct time{
    int hour;
    int minute;
    int second;
};

int main(){
    struct time t;
    int sec,t1;
    scanf("%d:%d:%d",&t.hour,&t.minute,&t.second);
    scanf("%d",&sec);
    t1=sec+t.second;
    if(t1>=60){               // Seconds greater than or equal to 60, Carry forward 
        t.second=t1-60;
        t.minute++;
        if(t.minute>=60){          // Minutes are greater than or equal to 60, Carry forward 
            t.minute=t.minute-60;
            t.hour++;
            if(t.hour>=24){
                t.hour-=24;
            }
        }
    }else{
        t.second=t1;
    }
    printf("%02d:%02d:%02d",t.hour,t.minute,t.second);
}

  summary :

  • As long as the clock is clear 、 minute 、 The problem of carry between seconds , It can be easily solved ;
  • Pay attention to the output format , use “%02d” To output .
原网站

版权声明
本文为[Xiaoyan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112218445235.html