Kattis - Best Relay Team - C Language I need an expert to work out the code for
ID: 3874488 • Letter: K
Question
Kattis - Best Relay Team - C Language
I need an expert to work out the code for the problem described below for me please. Thanks!
Best Relay Team You are the coach of the national athletics team and need to select which sprinters should represent your country in the 4 x 100 m relay in the upcoming championships. As the name of the event implies, such a sprit relay consist of 4 legs, 100 meters each. One would think that the best team would simply consist of the 4 fastest 100 m runners in the nation, but there is an important detail to take into account: flying start. In the 2nd, 3rd and 4th leg, the runner is already running when the baton is handed over. This means that some runners-those that have a slow acceleration phase can perform relatively better in a relay if they are on the 2nd, 3rd or 4th leg. Pict by Femando Frazão/Agéncia Brasil, cc by You have a pool of runners to choose from. Given how fast each runner in the pool is, decide which four runners should represent your national team and which leg they should run. You are given two times for each runner - the time the runner would run the 1st leg, and the time the runner would run any of the other legs. A runner in a team can only run one leg. Input The first line of input contains an integer n, the number of runners to choose from (4Explanation / Answer
For each player assuming as first player we have calculated the best pair.
then best pair is chosen from the best pairs set.
code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[1000];
float lap1;
float other_lap;
}player;
/*reading the players info from the file*/
void read_from_file(player **players, int * num)
{
int i;
FILE * fp = fopen("input.txt", "r");
fscanf(fp, "%d", num);
*players = (player *) malloc(sizeof(player) * (*num));
for( i = 0; i < *num; i++)
{
fscanf(fp, "%s%f%f", (*players)[i].name, &((*players)[i].lap1), &((*players)[i].other_lap));
}
}
void copy_player(player *p1, player *p2)
{
memset(p2->name, 0, 1000);
strcpy(p2->name, p1->name);
p2->lap1 = p1->lap1;
p2->other_lap = p1->other_lap;
}
void sortplayers(player ** players, int num)
{
int i, j;
player temp;
for(i = 0; i < num; i++)
{
for(i = 0; i < num -1; i++)
{
if((*players)[i].other_lap > (*players)[i+1].other_lap)
{
copy_player(&((*players)[i]), &temp);
copy_player(&((*players)[i]), &((*players)[i + 1]));
copy_player(&((*players)[i + 1]), &temp);
}
}
}
}
void find_other_lap_players(player *players, player *tempplayers, int num)
{
int i,j=1;
player *players1 = (player*) malloc(sizeof(player) * num);
memcpy(players1, players, sizeof(player) * num);
sortplayers(&players1, num);
for(i = 0; i < num; i++)
{
if(strcmp(players[i].name, tempplayers[0].name) == 0 )
continue;
tempplayers[j] = players[i];
j++;
if (j == 4)
{
free(players1);
return;
}
}
}
float cal_time(player *players)
{
return players[0].lap1 + players[1].other_lap + players[2].other_lap + players[3].other_lap;
}
/*find the best players*/
find_best_players(player *players, int num)
{
player bestplayers[4];
player tempplayers[4];
memset(bestplayers, 0, sizeof(bestplayers));
memset(tempplayers, 0, sizeof(tempplayers));
int i;
for(i = 0; i<num; i++)
{
copy_player(&players[i], &tempplayers[0]);
find_other_lap_players(players, tempplayers, num);
if (cal_time(bestplayers) > cal_time(tempplayers) || bestplayers[0].lap1 == 0.0)
{
copy_player(&tempplayers[0], &bestplayers[0]);
copy_player(&tempplayers[1], &bestplayers[1]);
copy_player(&tempplayers[2], &bestplayers[2]);
copy_player(&tempplayers[3], &bestplayers[3]);
}
}
printf("output: %f %s %s %s %s ",cal_time(bestplayers), bestplayers[0].name, bestplayers[1].name, bestplayers[2].name, bestplayers[3].name);
}
int main()
{
player * players;
player first;
player others[3];
int num;
/*reading data*/
read_from_file(&players, &num);
find_best_players(players, num);
}
Input file -> input.txt
5
rohit 1.1 2.3
jindal 1.0 2.4
ro 1.23 232.2
k2 1.23 232.32
k3 23.32 2.2
Sample output :
output:
238.130005
k2
rohit
jindal
ro
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.