求高人指点迷津
时间间隔
描述
现在给出一天内的两个时刻,时间间隔是时刻1与时刻2差的绝对值,时刻用时,分和秒表示。
输入n条时刻1和时刻2,统计并独立输出每个时间间隔,单位为秒。
输入格式
第一行输入整数n,之后输入n行,每行输入6个整数:h1,m1,s1,h2,m2和s2,
其中时刻1的时为h1,分为m1,秒为s1,时刻2的时为h2,分为m2,秒为s2。
输入数据保证:1<=n<=1000,0<=h1,h2<24,0<=m1,m2,s1,s2<60,
输出格式
每行输出时间间隔,单位为秒。
输入样例
1
23 45 40 22 50 30
输出样例
3310
仅供参考!
#include <stdio.h>
typedef struct ti
{
short h1;
short m1;
short s1;
short h2;
short m2;
short s2;
} ti;
void sub(ti *t, int n, int *out)
{
int m1, m2;
for (int i = 0; i < n; i++)
{
m1 = t[i].h1 * 3600 + t[i].m1 * 60 + t[i].s1;
m2 = t[i].h2 * 3600 + t[i].m2 * 60 + t[i].s2;
if (m1 > m2)
out[i] = m1 - m2;
else
out[i] = m2 - m1;
}
}
int main(int argc, char *argv[])
{
int n;
do
{
scanf("%d", &n);
} while (n < 1 || n > 1000);
ti a[n];
int out[n];
for (int i = 0; i < n; i++)
{
scanf("%hd%hd%hd%hd%hd%hd", &a[i].h1, &a[i].m1, &a[i].s1, &a[i].h2, &a[i].m2, &a[i].s2);
}
sub(a, n, out);
puts("");
for (int i = 0; i < n; i++)
{
printf("%d\n", out[i]);
}
}