Skip to content

Instantly share code, notes, and snippets.

@ryzokuken
Created December 7, 2016 17:52
Show Gist options
  • Save ryzokuken/f385e77c744813630b899450fb0fff48 to your computer and use it in GitHub Desktop.
Save ryzokuken/f385e77c744813630b899450fb0fff48 to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct Time {
int hours;
int minutes;
int seconds;
};
struct Runner {
char name[20];
int age;
struct Time start_time;
struct Time end_time;
};
int compareTime(struct Time t1, struct Time t2) {
if (t1.hours > t2.hours) {
return -1;
} else if (t1.hours < t2.hours) {
return 1;
} else {
if (t1.minutes > t2.minutes) {
return -1;
} else if (t1.minutes < t2.minutes) {
return 1;
} else {
if (t1.seconds > t2.seconds) {
return -1;
} else if (t1.seconds < t2.seconds) {
return 1;
} else {
return 0;
}
}
}
}
struct Time calculateDifference(struct Time t1, struct Time t2) {
struct Time result;
result.hours = t2.hours - t1.hours;
result.minutes = t2.minutes - t1.minutes;
result.seconds = t2.seconds - t1.seconds;
if (result.seconds < 0) {
result.minutes--;
result.seconds += 60;
}
if (result.minutes < 0) {
result.hours--;
result.minutes += 60;
}
return result;
}
void winner(struct Runner runner[]) {
for (i = 0; i < 19; i++) {
for (j = 0; j < 19 - i; j++) {
if (compareTime(calculateDifference(runner[j].start_time, runner[j].end_time), calculateDifference(runner[j + 1].start_time, runner[j + 1].end_time)) == -1) {
struct Runner temp;
temp = runner[j];
runner[j] = runner[j + 1];
runner[j + 1] = temp;
}
}
}
printf("%s\n", runner[0].name);
}
int main() {
struct Runner runner[20];
int i, j;
for (i = 0; i < 20; i++) {
scanf("%s", &runner[i].name);
scanf("%d", &runner[i].age);
scanf("%d:%d:%d", &runner[i].start_time.hours, &runner[i].start_time.minutes, &runner[i].start_time.seconds);
scanf("%d:%d:%d", &runner[i].end_time.hours, &runner[i].end_time.minutes, &runner[i].end_time.seconds);
}
winner(runner);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment