C语言问题，希望设计一个简结的。

@redraiment 你好，想跟你请教个问题：

8：00a.m.                              10:16 a.m.

9:43 a.m.                               11:52 a.m.

..............

(具体在程序中）

Enter a 24-hour time : 13:15

Closest departure time is 12:47 p.m., arriving at 3:00 p.m.

#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");
}

0

```#include <stdlib.h>
#include <stdio.h>

int schedule[][2] = {
{ 8 * 60 +  0, 10 * 60 + 16}, /* 08:00 => 10:16 */
{ 9 * 60 + 43, 11 * 60 + 52},
{11 * 60 + 19, 13 * 60 + 31},
{12 * 60 + 47, 15 * 60 +  0},
{14 * 60 +  0, 16 * 60 +  8},
{15 * 60 + 45, 17 * 60 + 55},
{19 * 60 +  0, 21 * 60 + 20},
{21 * 60 + 45, 23 * 60 + 58}
};
int except;

int cmp(const void* a, const void *b) {
int m = ((int*)a)[0];
int n = ((int*)b)[0];
return abs(m - except) - abs(n - except);
}

void time_convert(int timestamp, char* s) {
sprintf(s, "%d:%02d %c.m.", timestamp / 60 % 12, timestamp % 60, timestamp < 720? 'a': 'p');
}

int main(void) {
char from[16], to[16];
int hour, minute;

printf("Enter a 24-hour time: ");
scanf("%d:%d", &hour, &minute);

except = hour * 60 + minute;
qsort(schedule, sizeof(schedule) / sizeof(schedule[0]), sizeof(schedule[0]), cmp);

time_convert(schedule[0][0], from);
time_convert(schedule[0][1], to);

printf("Closest departure time is %s, arriving at %s.\n", from, to);

return EXIT_SUCCESS;
}```

0

0