The time interval for the brain

本文介绍了一种简单有效的方法来计算两个24小时制时间点之间的时间差,并通过C语言实现了一个实用的程序。该程序能够正确处理时间先后顺序,无论第二个时间点是否早于第一个时间点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。
Input
输入包括两行。
第一行为时间点1。
第二行为时间点2。
Output
以“小时:分钟:秒”的格式输出时间间隔。
格式参看输入输出(There is no blank line between the two lines of data)。
Example Input
12:01:12
13:09:43
Example Output
01:08:31

Although this problem is simple, but still stumped a lot of small partners, the key is to successfully convert the unit and then converted back.Many of the problems can use the idea of transformation to complete, or even a little conversion, you become the most familiar template.


//AC Code presented

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int h1,m1,s1,h2,m2,s2;
    int t1,t2,t;
    int ans1,ans2,ans3;
    char ch;
    scanf("%d%c%d%c%d\n",&h1,&ch,&m1,&ch,&s1);//Read
    scanf("%d%c%d%c%d",&h2,&ch,&m2,&ch,&s2);
    t1=3600*h1+60*m1+s1;//Calculate time 1
    t2=3600*h2+60*m2+s2;//Calculate time 2
    t=abs(t1-t2);//Does not rule out the time 2 than the time 1 early, so to calculate ABS
    ans3=t%60;//Convert the time difference into the form of the subject requirement
    ans2=((t-ans3)%3600)/60;
    ans1=(t-ans3-ans2*60)/3600;
    if (ans1<10) printf("0%d:",ans1); else printf("%d:",ans1);//The output format should be correct
    if (ans2<10) printf("0%d:",ans2); else printf("%d:",ans2);
    if (ans3<10) printf("0%d:",ans3); else printf("%d",ans3);
    return 0;
}

//Look at the code of the little partners, please do not copy and paste directly, even if the copy is responsible for their own.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nine_mink

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值