天天看点

UVa 10142 Australian Voting

#include <stdio.h>
#include <memory.h> 
#include <string.h>
char Candidates[20][80];
int Votes[1000][20];
void elect(int num,int voteNum)
{
	int Eliminate[20];
	int Count[20];
	int left,allvotes;
	int i,j;
	int min,max;
	left = num;
	memset(Eliminate,0,sizeof(Eliminate));
	while(left>1)
	{
		memset(Count,0,sizeof(Count));
		allvotes = 0;
		
		for(i=0;i<voteNum;i++)
		{
			for(j=0;j<num;j++)
			{	
				if(!Eliminate[ Votes[i][j]-1 ])
				{
					Count[ Votes[i][j]-1 ] ++;
					allvotes ++;
					break;
				}
			}
		}
		
		for(i=0,min=2000,max=0; i<num; i++){
			if(Count[i] > (allvotes/2))
			{
				printf("%s\n",Candidates[i]);
				return;
			} 
			if(max < Count[i] && !Eliminate[i])
				max = Count[i];
			if(min > Count[i] && !Eliminate[i])
				min = Count[i];
		}
		
		if(max == min) break;
		
		for(j=0; j<num; j++)
		{
			if(Count[j] == min) 
			{
				Eliminate[j] = 1;
				left --;
			}
		}	
	}
	
	for(i=0; i<num; i++)
		if(!Eliminate[i])
			printf("%s\n", Candidates[i]);
} 
int main()
{
	int cases,persons;
	char *tmp;
	char buf[100];
	int i,j;
	scanf("%d",&cases);
	getchar();
	while(cases--)
	{
		scanf("%d",&persons);
		getchar();
		memset(Candidates,' ',sizeof(Candidates));
		memset(Votes,0,sizeof(Votes));
		for(i=0;i<persons;i++)
		{
			gets(Candidates[i]);
		}
		for (i=0; gets(buf)!=NULL; i++)
		{
			if(!strcmp(buf,"")) break;
			for (j=0, tmp = buf; j < persons; j++, tmp = strchr(tmp, ' ') + 1)
        		sscanf(tmp, "%d", &Votes[i][j]);
		}
		elect(persons,i);
		if(cases>0)	printf("\n");
	}
	return 0;
}