- 论坛徽章:
- 0
|
下面的表格给出了一个城市到另一个城市的每日航班信息。
起飞时间 抵达时间
8:00a.m. 10:16 a.m.
9:43 a.m. 11:52 a.m.
..............
(具体在程序中)
编写一个程序,要求用户输入一个时间(用24小时制的时分表示)。 程序选择起飞时间与用户输入最接近的航班,显示出相应的起飞时间和抵达时间。
Enter a 24-hour time : 13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
提示:把输入用从午夜开始的分钟数表示。将这个时间与表格里也用从午夜开始的分钟数表示的起飞时间相比。例如:13:15从午夜开始是13X60 +15 =795分钟, 与下午12:47(从午夜开始是767分钟)最接近。
我自己编写了一个,但太过于繁琐,而且不太标准
#include <stdio.h>
#define time hour*60 + minute
int main()
{
int hour, minute;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hour, &minute);
if (0 < time && time <= ((9*60 +43) - 8*60)/2 + 8*60)
printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.\n");
else if (((9*60 +43) - 8*60)/2 + 8*60 < time && time <= ((11*60 + 19) - (9*60 +43))/2 + (9*60 +43))
printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.\n");
else if (((11*60 + 19) - (9*60 +43))/2 + (9*60 +43) < time && time <= ((12*60 + 47) - (11*60 + 19))/2 + (11*60 +19))
printf("Closest departure time is 11:19 a.m., arriving at 1:31 a.m.\n");
else if (((12*60 + 47) - (11*60 + 19))/2 + (11*60 +19) < time && time <= (14*60 - (12*60 +47))/2 + (12*60 +47))
printf("Closest departure time is 12:47 p.m., arriving at 3:00 p.m.\n");
else if (((14*60 - (12*60 +47))/2 + (12*60 +47)) < time && time <= ((15*60 + 45) - 14*60)/2 + (14*60))
printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.\n");
else if (((15*60 + 45) - 14*60)/2 + 14*60 < time && time <= (19*60 - (15*60 +45))/2 + (15*60 +45))
printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.\n");
else if ((19*60 - (15*60 +45))/2 + (15*60 +45) < time && time <= ((21*60 + 45) - 19*60)/2 + 19*60)
printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.\n");
else
printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.\n");
}
|
|