Algorithm:
step1: If arrival time in hour<current time in hour
then arrival time in hour+24
explanation: let current time is 10:00 & bus arrives at 9:00 ! This means you missed today’s bus which departed at 9:00. So you have to wait until next day(24 hours)
step2 : If arrival time in minute<current time in minute
then arrival time in minute+60 & arrival time in hour-1
explanation: let current time is 4:15 & bus will arrive at 4:20. In this case do nothing in this step. but if current time is 4:15 & bus will arrive at 5:10 then arrival time in minute will be increased with 60 (10+60) & arrival time in hour will be reduced by 1 (5-1). Now we can calculate the time to arrive a bus as step3.
step3: How much time it will take to arrive bus=(arival time in hour – current time in hour) * 60 +(arival time in minute– current time in minute)
step4:How much time it will take to arrive home=How much time it will take to arrive bus(calculated in step two)+ traveling time.
step5:Find the lowest value of step4’s values. Show this lowest value as output
#include<stdio.h>
#include<stdlib.h>
int main() {
long tc, bus, i, j, ch, cm, ah, am, dh, dm, x, count, result, trvl_time[101];
char crnt_time[8], arvl_time[101][8], d[3];
scanf("%ld", & tc);
for (i = 1; i <= tc; i++) {
scanf("%ld %s", & bus, crnt_time);
count = 0;
d[0] = crnt_time[0];
d[1] = crnt_time[1];
d[2] = '\0';
ch = atoi(d);
d[0] = crnt_time[3];
d[1] = crnt_time[4];
d[2] = '\0';
cm = atoi(d);
for (j = 1; j <= bus; j++) {
scanf("%s %ld", arvl_time[j], & trvl_time[j]);
count++;
d[0] = arvl_time[j][0];
d[1] = arvl_time[j][1];
d[2] = '\0';
ah = atoi(d);
d[0] = arvl_time[j][3];
d[1] = arvl_time[j][4];
d[2] = '\0';
am = atoi(d);
if (ah < ch || (ah == ch && am < cm)) {
if (cm > 0) {
dm = 60 - cm;
dh = 23 - ch;
} else
dh = 24 - ch;
dm += am;
dh += ah;
x = dh * 60 + dm + trvl_time[j];
if (count == 1)
result = x;
if (x < result)
result = x;
} else {
if (am < cm) {
dm = am + 60 - cm;
ah--;
} else
dm = am - cm;
dh = ah - ch;
x = dh * 60 + dm + trvl_time[j];
if (count == 1)
result = x;
else if (x < result)
result = x;
}
}
printf("Case %ld: %ld\n", i, result);
}
return 0;
}
Next Previous