Разумный ответ требует использования массивов и функций. Вы, похоже, тоже не очень хорошо выглядите рядом.
Данные можно представить так, используя макрос, чтобы скрыть повторяющиеся вычисления:
#define TIME(hh, mm) ((hh) * 60 + (mm))
static const int dep[] = { TIME( 8, 0), TIME( 9, 45), TIME(11, 19), TIME(12, 47),
TIME(14, 0), TIME(15, 45), TIME(19, 0), TIME(19, 45) };
static const int arr[] = { TIME(10, 16), TIME(11, 52), TIME(13, 31), TIME(15, 0),
TIME(16, 8), TIME(17, 55), TIME(21, 20), TIME(23, 58) };
enum { NUM_TIMES = sizeof(dep) / sizeof(dep[0]) };
Вам также следует использовать функцию для форматирования времени в виде строки. Обратите внимание, что время am / pm странное; Вы должны заменить 12 на 0 (поэтому 12:59 утра наступает незадолго до 1:00 утра). Функция предполагает, что переданный буфер достаточно большой (минимум 9 байт), чтобы принять отформатированную строку.
char *timestr(int hhmm, char *buffer)
{
int hh = (hhmm / 60) % 12;
if (hh == 0)
hh = 12;
sprintf(buffer, "%.2d:%.2d %s", hh, hhmm % 60, (hhmm < TIME(12, 0)) ? "am" : "pm");
return buffer;
}
Теперь, учитывая прочитанное значение, вы можете найти правильное значение довольно просто:
void find_and_print_nearest_time(int time)
{
int i = 0;
if (time < dep[i])
// Before first departure
print_times(time, dep[i], arr[i]);
else if (time >= dep[NUM_TIMES-1])
// After last departure
print_times(time, dep[NUM_TIMES-1], arr[NUM_TIMES-1]);
else
{
// Somewhere in the middle
for (i = 0; i < NUM_TIMES - 1; i++)
{
if (time >= dep[i+1])
continue;
if (time < (dep[i] + dep[i+1]) / 2)
print_times(time, dep[i], arr[i]);
else if (time < dep[i+1])
print_times(time, dep[i+1], arr[i+1]);
break;
}
}
}
void print_times(int u_time, int dep, int arr)
{
char buffer1[9];
char buffer2[9];
char buffer3[9];
printf("Given %s, the nearest departure time is %s, arriving at %s\n",
timestr(u_time, buffer1), timestr(dep, buffer2), timestr(arr, buffer3));
}
Оставив только с main()
написать:
#include <stdio.h>
int main(void)
{
int hours, minutes, time;
// Get the user's time
printf("Enter a 24 hour time (hh:mm): \n");
if (scanf("%d:%d", &hours, &minutes) == 2)
{
time = hours * 60 + minutes;
find_and_print_nearest_time(time);
}
return(0);
}
Или с циклом (проще для тестирования!):
int main(void)
{
int hours, minutes, time;
while (scanf("%d:%d", &hours, &minutes) == 2)
{
// Should check 0 <= hours <= 23
// Should check 0 <= minutes <= 59
// Might accept 24:00
time = TIME(hours, minutes);
find_and_print_nearest_time(time);
}
return(0);
}
При тестировании я собирал вещи в несколько ином порядке, но код сработал так, как я ожидал в первый раз. Предоставлены входные данные:
00:00
06:00
08:00
08:50
08:51
08:52
08:53
08:54
08:55
09:42
09:43
09:44
10:30
10:31
10:32
10:33
14:50
14:51
14:52
14:53
14:54
14:55
21:44
21:45
21:46
23:59
Вывод был:
Given 12:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 06:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:50 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:51 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:52 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:53 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:54 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:55 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:42 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:43 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:44 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:30 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:31 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:32 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 10:33 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 02:50 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:51 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:52 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:53 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:54 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:55 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 09:44 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:45 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:46 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 11:59 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm