vote.c
contents ::
  vote.c
  input

/**********************************************
 *                                            *
 *   James Little Programming contest         *
 *              Question F                    *
 *   *Vote Counting Problem 30 Points         *
 *                                            *
 *                                            *
 *                                            *
 *                                            *
 *                                            *
 *                                            *
 **********************************************/

#include <stdio.h>
#include <stdlib.h>

typedef struct candidate {
  char name[20];
  int count;
} candid;

int duplicate(candid *players, char *buffer, int count){
  int i=0;
  for(i = 0; i<count; i++){
    if(strcmp(players[i].name, buffer) == 0){
      players[i].count++;
      return 0;
    }
  }
  return 1;


main(){
  int i = 0;
  int cand_max = 5;
  int cand_count = 0;
  char buffer[20];
  int max_votes = 0;
  int winners_count = 1;
  

  candid players[cand_max];
  candid winners[cand_max];

  while(1){
    while(cand_count<cand_max){
      scanf("%s", buffer);
    vote_count:
      if(strcmp("#", buffer) == 0)
         break;
      if(duplicate(players, buffer, cand_count)){
         strcpy(players[cand_count].name, buffer);
         players[cand_count].count = 1;
         cand_count++;
      }
    }
    
    // FIND WINNERS:
    i=0;
    while(i<cand_count){
      if(players[i].count >= max_votes){
         winners[winners_count++] = players[i];
         max_votes = players[i].count;
      }
      i++;
    }
    
    // find a second place
    if(winners_count == 2){
      int second_max = 0;
      i=0;
      while(i<cand_count){
         if(players[i].count > second_max && players[i].count != max_votes){
           winners[0] = players[i];
           second_max = players[i].count;
         }
         i++;
      }
      
    }
    
    if(winners[winners_count-1].count != winners[winners_count-2].count){
      printf("%s wins by %d vote%s\n",winners[winners_count-1].name,
              winners[winners_count-1].count-winners[winners_count-2].count,
              (winners[winners_count-1].count-winners[winners_count-2].count>1)?
              "s." : ".");
    } else {
      printf("a tie between %s", winners[--winners_count].name);
      while(winners_count>1){
         printf(" and %s", winners[--winners_count].name);
      }
      printf(".\n");
    }
    
    cand_count = 0;
    max_votes = 0;
    winners_count = 1;

    scanf("%s", buffer);
    if(strcmp("##", buffer) == 0)
      break;
    else
      goto vote_count;
  }
  return EXIT_SUCCESS;
}

James Little