C语言实验——时间间隔
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。
Input
输入包括两行。
第一行为时间点1。
第二行为时间点2。
第一行为时间点1。
第二行为时间点2。
Output
以“小时:分钟:秒”的格式输出时间间隔。
格式参看输入输出。
格式参看输入输出。
Example Input
12:01:12 13:09:43
Example Output
01:08:31
解题思路:题目要求很简单,就是算出两个时刻的差值,把这个差值转换成小时、分钟、秒然后表示出来,所以我们要先算出两个时刻的差值,我用del来表示了,做这个题的时候可以类比十进制数的取每个位上的值,原理是一样的。然后用%02d来表示出来就完成了。
#include<stdio.h> int main() { int h1, h2, m1, m2, s1, s2, t1, t2, del; t1 = t2 = 0; scanf("%d:%d:%d",&h1, &m1, &s1); scanf("%d:%d:%d",&h2, &m2, &s2); t1 = h1 * 3600 + m1 * 60 + s1; t2 = h2 * 3600 + m2 * 60 + s2; if(t1 > t2) { del = t1 - t2; } else { del = t2 - t1; } int a, b, c; a = del / 3600; b = del / 60 % 60; c = del % 60; printf("%02d:%02d:%02d\n",a, b, c); return 0; }