Created
April 26, 2018 14:27
-
-
Save kahless62003/a402387a659e7c67c7eeef7210d82de9 to your computer and use it in GitHub Desktop.
Cup Stacking exercise from Kattis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
Stacking Cups | |
You are programming a cup stacking module for your robot. This robot is equiped with several sensors that can accurately determine the radius and color of a cup. The problem is that there is a glitch in the robot’s core routine that processess sensor inputs so the radius is doubled, if the result of the color sensor arrives to the routine after the radius. | |
For instance, for a red cup with a radius of 5 units, your module will receive either "red 5" or "10 red" message. | |
Given a list of messages from the core routine, each describing a different cup, can you put the cups in order of the smallest to the largest? | |
Input | |
The first line of the input file contains an integer $N$, the number of cups ($1 \leq N \leq 20$). | |
* Next N lines will contain two tokens each, either as "color radius" or "diameter color". | |
* The radius of a cup R will be a positive integer less than 1000. | |
* The color of a cup C will be a non-empty string of lower case English letters of length at most $20$. | |
* All cups will be different in both size and color. | |
Output | |
Output colors of cups, one color per line, in order of increasing radius. | |
Sample Input 1 Sample Output 1 | |
3 | |
red 10 | |
10 blue | |
green 7 | |
blue | |
green | |
red | |
* | |
*/ | |
#include <stdio.h> /*basic io*/ | |
#include <stdlib.h> /*qsort/malloc/free*/ | |
#include <ctype.h> /*isdigit*/ | |
//Global typedef structure so qsort compare function knows about it | |
typedef struct Cups | |
{ | |
char colour[24]; | |
int radius; | |
} cup; | |
/*qsort compare function*/ | |
int cmpfunc (const void * a, const void * b) | |
{ | |
cup *cupA = (cup *)a; | |
cup *cupB = (cup *)b; | |
//return ( cupB->radius - cupA->radius ); /*sorts big to small*/ | |
return ( cupA->radius - cupB->radius ); /*sorts small to big*/ | |
} | |
int main(void) | |
{ | |
#define BUFSIZE 64 | |
int line_counter = 0, number_of_cups = 0; | |
//char filename_test[]="cups_sample_input.txt"; | |
char line[BUFSIZE]; | |
// for dynamic allocation | |
cup *cuparray; | |
//FILE *fp; | |
//fp = fopen (filename_test, "r"); | |
//if (fp == NULL) | |
//{ | |
// fputs("Failed to open file.\n", stderr); | |
// return 1; | |
//} | |
if (fgets(line,BUFSIZE,stdin) != NULL) | |
{ | |
if (isdigit(line[0]) != 0) | |
{ | |
if (sscanf(line, "%i", &number_of_cups)==1) | |
{ | |
// DEBUG print | |
fprintf(stderr, "\ncups=%i\n", number_of_cups); | |
} | |
else | |
{ | |
fputs("Failed to scan number of cups.\n", stderr); | |
return 1; | |
} | |
} | |
else | |
{ | |
fputs("IsNotDigit.\n", stderr); | |
return 1; | |
} | |
} | |
// Can't declare this array until we've read the first line and know how big the file says it needs to be. | |
//static | |
//cup cuparray[number_of_cups]; | |
//dynamic | |
cuparray = malloc( number_of_cups * sizeof(cup) ); | |
if(cuparray==NULL) | |
{ | |
fputs("Error: memory not allocated for array.\n", stderr); | |
return 1; | |
} | |
// Now scan the lines into the array of structures. | |
// DEBUG print | |
fputs("\nReading data...\n\n", stderr); | |
while(fgets(line,BUFSIZE,stdin) != NULL) | |
{ | |
// If the first char is a digit, assume diameter-colour line | |
if (isdigit(line[0]) != 0) | |
{ | |
if (sscanf(line, "%i %s", &cuparray[line_counter].radius, cuparray[line_counter].colour)==2) | |
{ | |
// extra step of halving diameter to make it a radius | |
cuparray[line_counter].radius = cuparray[line_counter].radius/2; | |
// DEBUG print | |
fprintf(stderr, "colour=%s, radius(halved-diameter)=%i\n", cuparray[line_counter].colour, cuparray[line_counter].radius); | |
} | |
else | |
{ | |
fputs("Failed to scan diameter colour line.\n", stderr); | |
return 1; | |
} | |
} | |
// else assume colour-radius line | |
else | |
{ | |
if (sscanf(line, "%s %i", cuparray[line_counter].colour, &cuparray[line_counter].radius)==2) | |
{ | |
// DEBUG print | |
fprintf(stderr, "colour=%s, radius=%i\n", cuparray[line_counter].colour, cuparray[line_counter].radius); | |
} | |
else | |
{ | |
fputs("Failed to scan colour radius line.\n", stderr); | |
return 1; | |
} | |
} | |
line_counter++; | |
} | |
// DEBUG print | |
fputs("\nSorting data...\n", stderr); | |
qsort(cuparray, (size_t)number_of_cups, sizeof(cup), cmpfunc); | |
// DEBUG print | |
fputs("\nPrinting sorted data...\n\n", stderr); | |
for (line_counter = 0; line_counter<number_of_cups; line_counter++) | |
{ | |
printf("colour=%s, radius=%i\n", cuparray[line_counter].colour, cuparray[line_counter].radius); | |
} | |
// FINAL OUTPUT | |
fputs("\nPrinting final output...\n\n", stderr); | |
for (line_counter = 0; line_counter<number_of_cups; line_counter++) | |
{ | |
printf("%s\n", cuparray[line_counter].colour); | |
} | |
//free malloc'ed array | |
free(cuparray); | |
//close file | |
//fclose(fp); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment