#include #include #include #include #define __INPUT_FILE_NAME__ "ballot.in" struct party_t { char name[24]; int promille; }; struct list_t { struct party_t *val; struct list_t *next; }; struct list_t *list_create( struct party_t const*const head ) { struct list_t *elem = (struct list_t*) malloc(sizeof(struct list_t)); struct party_t *copy = (struct party_t*)malloc(sizeof(struct party_t)); memcpy(copy, head, sizeof(struct party_t)); elem->val = copy; elem->next = 0; return elem; } void list_insert( struct list_t ** head, struct party_t const*const val ) { struct list_t *curr = *head; struct list_t *elem = (struct list_t*) malloc(sizeof(struct list_t)); struct party_t *copy = (struct party_t*)malloc(sizeof(struct party_t)); memcpy(copy, val, sizeof(struct party_t)); elem->val = copy; while( curr ) { struct list_t *next = curr->next; if( strcmp(elem->val->name, curr->val->name) < 0 ) { elem->next = *head; *head = elem; break; } else if( next && strcmp(elem->val->name, next->val->name) < 0 ) { elem->next = next; curr->next = elem; break; } else if( !next ) { elem->next = 0; curr->next = elem; break; } else curr = next; } } int list_find( struct list_t const* head, char const* id ) { int c; while( head ) { for( c = 0; c < 20; ++c ) if( head->val->name[c] != id[c] || !id[c] || !head->val->name[c] ) break; if( id[c] == head->val->name[c] ) return head->val->promille; if( head->val->name[c] > id[c] ) return -1; head = head->next; } return -1; } int main( int argsc __attribute__((unused)), char ** argsv __attribute__((unused)) ) { char name[24]; int p, P, g, G; struct list_t *list; assert( freopen(__INPUT_FILE_NAME__, "rb", stdin) ); assert( fscanf( stdin, "%d %d", &P, &G ) == 2 ); for( p = 0; p < P; ++p ) { int a, b; struct party_t c; assert( fscanf( stdin, "%s %d.%d", &c.name[0], &a, &b ) == 3 ); c.promille = a*10 + b; if( !p ) list = list_create( &c ); else list_insert( &list, &c ); } for( g = 0; g < G; ++g ) { int lval = 0; int cval; int rval; char operator[4]; do { assert( fscanf(stdin, "%s %s", &name[0], &operator[0]) == 2 ); cval = list_find( list, name ); assert( cval >= 0 && "party does not exist" ); lval += cval; } while( operator[0] == '+' ); assert( fscanf(stdin, "%d", &rval) == 1 ); fprintf( stdout, "Guess #%d was ", g+1 ); rval *= 10; if( operator[0] == '>' ) { if( operator[1] == '=' ) fprintf( stdout, (lval>=rval)? "correct" : "incorrect" ); else fprintf( stdout, (lval >rval)? "correct" : "incorrect" ); } else if( operator[0] == '=' ) fprintf( stdout, (lval==rval)? "correct" : "incorrect" ); else { if( operator[1] == '=' ) fprintf( stdout, (lval<=rval)? "correct" : "incorrect" ); else fprintf( stdout, (lval< rval)? "correct" : "incorrect" ); } fprintf( stdout, ".\n" ); } return EXIT_SUCCESS; }