poj2643 (STL中map的使用)

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.

intput

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

output

Rhinoceros

题目大意,
n个人有好几个个党派,投票出党派最多人的党派,
(使用map形成映射)

下面给出AC代码以及代码注释

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
	map<string,string>p;
	map<string,int>q;//形成相对应关系 
	p.clear();
	q.clear();//清空 
	int n,m;
	int i,j,max,flag;
	max=0;flag=0; 
	string s1,s2,name,temp;
	cin>>n;
	getchar();//吸收回车 
	for(i=0;i<n;i++)
	{
		getline(cin,s1);
		getline(cin,s2);//输入 
		p[s1]=s2;//名字和党派形成对应 
	}
	cin>>m;
	getchar();
	for(int j=0;j<m;j++) 
	{
		getline(cin,name);//输入投票清况 
		q[name]++;//对应的党派数量增加 
		if(q[name]>max)//找出最大党派 
		{
			max=q[name];
			temp=p[name];
			flag=1;//做标记 
		}
		else if(q[name]==max)
		{
			flag=0;
		}
		
	}
	if(flag==1)//根据标记结果输出 
	cout<<temp<<endl;
	else
	cout<<"tie"<<endl;
	return 0;
}

谢谢支持!一键三连!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以得知这是一道POJ(Peking University Online Judge)的题目,题号为3213。这道题目的主要任务是计算给定的一组数据,每个数出现的次数,并输出出现次数最多的数的出现次数。同时,由于测试数据较大,使用C++的iostream对象或Java的Scanner对象可能会导致效率问题。 下面是Java语言的解题思路和代码实现: 1.首先,我们需要使用Java的HashMap来存储每个数出现的次数。HashMap是一种基于哈希表实现的Map接口,可以用来存储键值对,其键是唯一的,值可以重复。 2.接下来,我们需要读入数据,并将每个数出现的次数存储到HashMap。由于输入数据的格式比较特殊,我们需要使用Java的Scanner类来读取数据。具体来说,我们可以使用Scanner类的nextInt()方法来读取整数,并使用while循环来读取所有的数据。 3.最后,我们需要遍历HashMap,找到出现次数最多的数的出现次数,并输出结果。 下面是Java语言的代码实现: ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x != -1) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } } int max = 0; for (int key : map.keySet()) { max = Math.max(max, map.get(key)); } System.out.println(max * m); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值