#include #include #include int ncandidates; char **candidates; int *tally; int nvotes; char **votes; int compare (const void * a, const void * b) { char *s1 = (char *) (*(char **)a); char *s2 = (char *) (*(char **)b); return strcmp(s1,s2); } int FindCandidate(char *s) { int low=0,high=ncandidates-1; int try; while (low != high) { try = (low + high)/2; if (strcmp(s,candidates[try]) < 0) { high = try-1; } if (strcmp(s,candidates[try]) > 0) { low = try+1; } if (strcmp(s,candidates[try]) == 0) { return try; } } return (low); } main() { int i,j; char line[1024]; while(1){ int done=0; fscanf(stdin,"%d %d\n",&ncandidates,&nvotes); if(ncandidates == 0) exit(0); candidates = (char **) malloc(ncandidates * sizeof(char*)); tally = (int *) malloc(ncandidates * sizeof(int)); votes = (char **) malloc(nvotes * sizeof(char*)); i=j=0; while (i maxvotes) maxvotes = tally[j]; if (tally[j] < minvotes) minvotes = tally[j]; } } // did someone get greater than 50% of the vote? if (maxvotes > counted/2) { for (j=0 ; j 1) printf(" and "); else printf("\n"); } } done=1; } // if not... find candidate(s) with fewest votes and eliminate him(them) for (j=0 ; !done && j -1) printf ("eliminating %s\n",candidates[j]); tally[j] = -1; } else tally[j] = 0; } } } }