PAT选择结构基础 7-9 sdut-C语言实验-时间间隔

从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。

输入格式:

输入包括两行。
第一行为时间点1。
第二行为时间点2。

输出格式:

以“小时:分钟:秒”的格式输出时间间隔。
格式参看输入输出。

输入样例:

12:01:12
13:09:43

输出样例:

01:08:31
错误示例:穷举法(下面)

#include<stdio.h>
int main()
{
	int hour1, hour2, minute1, minute2, second1, second2;
	scanf("%d:%d:%d", &hour1, &minute1, &second1);//输入%d:%d:%d表示输入08:40:25这种时间值
	scanf("%d:%d:%d", &hour2, &minute2, &second2);
	int hour, minute, second;
	if (hour1 > hour2)//二十七种情况列举
	{
		hour = hour1 - hour2;
		if (minute1 > minute2)
		{
			minute = minute1 - minute2;
			if (second1 > second2)
			{
				second = second1 - second2;
			}
			else if (second1 == second2)
			{
				second = 0; 
			}
			else
			{
				second = second1 - second2 + 60;
				minute--;
			}
		}
		else if (minute1 == minute2)
		{
			minute = 0;
			if (second1 > second2)
			{
				second = second1 - second2;
			}
			else if (second1 == second2)
			{
				second = 0; 
			}
			else
			{
				second = second1 - second2 + 60;
				hour--;
				minute = 59;
			}
		}
		else
		{
			minute = minute1 - minute2 + 60;
			hour--;
			if (second1 > second2)
			{
				second = second1 - second2;
			}
			else if (second1 == second2)
			{
				second = 0; 
			}
			else
			{
				second = second1 - second2 + 60;
				minute--;
			}
		}
	}
	else if (hour1 == hour2)
	{
		hour = 0;
		if (minute1 > minute2)
		{
			minute = minute1 - minute2;
			if (second1 > second2)
			{
				second = second1 - second2;
			}
			else if (second1 == second2)
			{
				second = 0;
			}
			else
			{
				second = second1 - second2 + 60;
				minute--;
			}
		}
		else if (minute1 == minute2)
		{
			minute = 0;
			if (second1 > second2)
			{
				second = second1 - second2;
			}
			else if (second1 == second2)
			{
				second = 0;
			}
			else
			{
				second = second2 - second1;
			}
		}
		else
		{
			minute = minute2 - minute1;
			if (second2 > second1)
			{
				second = second2 - second1;
			}
			else if (second1 == second2)
			{
				second = 0;
			}
			else
			{
				second = second2 - second1 + 60;
				minute--;
			}
		}
	}
	else
	{
		hour = hour2 - hour1;
		if (minute2 > minute1)
		{
			minute = minute2 - minute1;
			if (second2 > second1)
			{
				second = second2 - second1;
			}
			else if (second1 == second2)
			{
				second = 0; 
			}
			else
			{
				second = second2 - second1 + 60;
				minute--;
			}
		}
		else if (minute1 == minute2)
		{
			minute = 0;
			if (second2 > second1)
			{
				second = second2 - second1;
			}
			else if (second2 == second1)
			{
				second = 0; 
			}
			else
			{
				second = second2 - second1 + 60;
				hour--;
				minute = 59;
			}
		}
		else
		{
			minute = minute2 - minute1 + 60;
			hour--;
			if (second2 > second1)
			{
				second = second2 - second1;
			}
			else if (second2 == second1)
			{
				second = 0; 
			}
			else
			{
				second = second2 - second1 + 60;
				minute--;
			}
		}
	}
	printf("%02d:%02d:%02d", hour, minute, second);//%02d表示两个长度,不够补0
	return 0;
}

正确思路,通过转化,将其转化为秒计时,再对其进行加减运算。

#include<stdio.h>
int main()
{
    int a,b,c,x,y,z,m,n,t;
    scanf("%d:%d:%d\n%d:%d:%d",&a,&b,&c,&x,&y,&z);
    m=3600*a+60*b+c;
    n=3600*x+60*y+z;
    if(m>n)
        t=m-n;
    else
        t=n-m;
    printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值