Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为–03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。
Input
输入包括两行。
第一行为时间点1。
第二行为时间点2。
Output
以“小时:分钟:秒”的格式输出时间间隔。
格式参看输入输出。
Sample Input
12:01:12
13:09:43
Sample Output
01:08:31
Hint
Source
注意本题中时间不能直接相加减,必须同一单位,化为秒,再相加减
#include <stdio.h>
int main()
{
int a1,a2,a3;
int b1,b2,b3;
long c1,c2,c3;
//因为将时间化为秒后数据会比较大,所以可以定义为long型;
int d1,d2,d3;
scanf("%2d:%2d:%2d",&a1,&a2,&a3);
scanf("%2d:%2d:%2d",&b1,&b2,&b3);
c1=a1*3600+a2*60+a3;
c2=b1*3600+b2*60+b3;
//将时间换算为秒;
if(c1>c2)
c3=c1-c2;
else
c3=c2-c1;
//加一个条件判断保证减出来的时间是正数;
d1=c3/3600;
//因为两个整数相处其结果只保留整数,所以相减后的时间除以3600即为整小时;
d2=c3/60%60;
//c3除以60后,便除掉了整小时的时间,再对60取余便是整分钟;
d3=c3%60;
//c3对60取余便是余下的秒钟;
printf("%02d:%02d:%02d",d1,d2,d3);
//"%0nd"即为输出为两位且不足两位的用“0”补齐;
return 0;
}
12:01:12
13:09:43
01:08:31
Process returned 0 (0x0) execution time : 3.340 s
Press any key to continue.