Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3991 | Accepted: 1869 |
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.
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
Sample Output
Rhinoceros
Source
Waterloo local 1999.06.19
题目大意就是选举,然后有那些人要选,每个人有党派或者没党派。有多少选民要选他们。最后输出赢的党派。如果是无党派赢,则输出“独立”,如果前几名选票一样多,则输出“tie”.
直接用马屁,先把人跟党派关联一起,再把选民选的人跟int 关联一起用于计数。
map 用起来比数组更厉害。将字符串转换成int 等其他类型的
#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;
map <string ,int > vote;
map <string ,string > con;
struct node{
char name[90];
char party[90];
int c;
}f[90];
int num=0;
int n,i,m;
bool ok;
string ans;
char str[100];
int main()
{
int i;
scanf("%d",&n);
getchar();
ok=true;
vote.clear();
for( i=1;i<=n; i++)
{
gets(f[i].name);
gets(f[i].party);
con[f[i].name]=f[i].party;
}
scanf("%d",&m);
getchar();
while(m--)
{
gets(str);
vote[str]++;
}
map<string ,int >::iterator ite;
for(ite=vote.begin();ite!=vote.end();ite++)
{
if(ite->second> num)
{
num=ite->second;
ans=ite->first;
ok=false; //如果更新了最大值,重置ok.
}
else
if(ite->second==num) //如果有等于的情况,说明有一样多选票存在,但可能不是最多的选票
ok=true;
}
if(ok)
cout<<"tie"<<endl;
else
cout<<con[ans]<<endl;
return 0;
}
更多推荐
POJ 2643 Election
发布评论