poj 2643 stl_map(竞选啦)

题意:一些人竞选。首先给出n个人-党对,一个人属于一个党派,人和党派最多出现一次。党派可能是independent(这个可以多于一个)。接着给出m个选票,每张选票是一个人名。输出选票最多的那个人对应的党派,如果有两个人得票同样多,则输出tie。

思路:用stl的map数据结构就容易了。用一个map<string,string>来存放人名子和党派的映射,再用一个map<string,int>num计数即可。注意string类需要用cout输出,如果想用printf输出则需对字符串调用.c_str()方法。不用stl的话就用一个结构体绑定即可。注意:list中出现的名字有可能没在name-patry中出现。

stl:

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#define N 88
using namespace std;
char s[N],t[N];
int n;
map<string,string>name;
map<string,int>num;
int main(){
	freopen("a.txt","r",stdin);
	while(scanf("%d\n",&n)!=EOF){
		int i,j,max,tie;
		string temp;
		for(i = 0;i<n;i++){
			gets(s);
			gets(t);
			name[s] = t;	
		}
		scanf("%d\n",&n);
		max = tie = 0;
		for(i = 0;i<n;i++){
			gets(s);
			num[s]++;
			if(num[s] > max){
				max = num[s];
				temp = s;
				tie = 0;
			}else if(num[s] >= max)
				tie = 1;
		}
		if(tie)
			printf("tie\n");
		else
			printf("%s\n",name[temp].c_str());
			//cout<<name[temp]<<endl;
	}
	return 0;
}

struct:

#include <stdio.h>
#include <string.h>
#define N 88
struct person{
	char name[N],party[N];
	int num;
}p[27];
int n,m;
int find(char x[N]){
	int i;
	for(i = 0;i<n;i++)
		if(!strcmp(p[i].name,x))
			return i;
	return -1;
}
int main(){
	freopen("a.txt","r",stdin);
	while(scanf("%d",&n)!=EOF){
		int i,j,id,max,tie;getchar();
		for(i = 0;i<n;i++){
			gets(p[i].name);
			gets(p[i].party);
			p[i].num=0;
		}
		scanf("%d",&m);getchar();
		id = max = 0;
		tie = 1;
		for(i = 0;i<m;i++){
			char temp[N];
			gets(temp);
			j = find(temp);
			if(j == -1)//有可能list中的没在name-patry中出现
				continue;
			p[j].num++;
			if(p[j].num > max){
				max = p[j].num;
				id = j;
				tie = 0;
			}else if(p[j].num == max)
				tie = 1;
		}
		if(tie)
			printf("tie\n");
		else
			printf("%s\n",p[id].party);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值