Description
Canada has a multi-party system of government. Each candidate is generally associated with a party, and the party whose candidates win the most ridings generally forms the government. Some candidates run as independents, meaning they are not associated with any party. Your job is to count the votes for a particular riding and to determine the party with which the winning candidate is associated.
Input
The first line of input contains a positive integer n satisfying 2 <= n <= 20, the number of candidates in the riding. n pairs of lines follow: the first line in each pair is the name of the candidate, up to 80 characters; the second line is the name of the party, up to 80 characters, or the word "independent" if the candidate has no party. No candidate name is repeated and no party name is repeated in the input. No lines contain leading or trailing blanks.
The next line contains a positive integer m <= 10000, and is followed by m lines each indicating the name of a candidate for which a ballot is cast. Any names not in the list of candidates should be ignored.
The next line contains a positive integer m <= 10000, and is followed by m lines each indicating the name of a candidate for which a ballot is cast. Any names not in the list of candidates should be ignored.
Output
Output consists of a single line containing one of:
- The name of the party with whom the winning candidate is associated, if there is a winning candidate and that candidate is associated with a party.
- The word "independent" if there is a winning candidate and that candidate is not associated with a party.
- The word "tie" if there is no winner; that is, if no candidate receives more votes than every other candidate.
Sample Input
3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 6 John Smith Marilyn Manson Marilyn Manson Jane Doe John Smith Marilyn Manson
分析:
这是一个比较简单的题目,我没有用STL去做,用的结构体,也算是练习一下熟练度吧。
值得注意的是要充分运用标记,当没有任何一个候选人票数大于其他所有候选人时,输出的是tie
记得标记最大票数和最大票数候选人的下标。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include<stdio.h>
#include<string.h>
struct S
{
char a[100],b[100];
} t[30];
int main()
{
int m,n,i,j,k=0,f,s[100],flag=0;
char c[100];
memset(s,0,sizeof(s));
scanf("%d",&n);
getchar();
for(i=0; i<n; i++)
{
gets(t[i].a);
gets(t[i].b);
}
scanf("%d",&m);
getchar();
for(i=0; i<m; i++)
{
gets(c);
for(j=0; j<n; j++)
{
if(strcmp(c,t[j].a)==0)
s[j]++;
if(s[j]>k)
{
k=s[j];
f=j;
}
}
}
for(i=0; i<n; i++)
if(s[i]==k&&i!=f)
flag=1;
if(flag==0)
for(i=0; i<n; i++)
if(s[i]==k)
printf("%s\n",t[i].b);
if(flag==1)
printf("tie\n");
return 0;
}