poj 2643 stl_map(竞选啦)
来源:互联网 发布:手机淘宝怎么买游戏号 编辑:程序博客网 时间:2024/04/30 11:06
题意:一些人竞选。首先给出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 88using 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");elseprintf("%s\n",name[temp].c_str());//cout<<name[temp]<<endl;}return 0;}
struct:
#include <stdio.h>#include <string.h>#define N 88struct 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");elseprintf("%s\n",p[id].party);}return 0;}
0 0
- poj 2643 stl_map(竞选啦)
- STL_map
- STL_map
- STL_MAP
- STL_map
- STL_map
- STL_map用法
- stl_map.h
- STL_map操作
- STL_map用法
- 第二次竞选
- 竞选部长
- poj 1002 Java AC啦
- STL_MAP容器常用操作
- stl_map,set 用法
- STL_Map用法详解
- poj2503-Babelfish-STL_map
- 竞选学生会主席
- linux 下安装redis
- cannot set user id: Resource temporarily unavailable
- 《JavaScript高级程序设计 第三版》学习笔记 (十)DOM详解
- iOS 代码实现获得应用的版本号(Version/Build)
- 小米采用意法半导体低功耗电磁罗盘模块LSM303D
- poj 2643 stl_map(竞选啦)
- jasperreport ireport PDF中文字体完美解决方案
- 有关于子控件填充容器的异常说明
- 项目管理实战十步法——中国印钞造币总公司培训记
- 接口与多态实现功能的扩展
- C++中explicit关键字的作用
- 将PDF文件Open In MyApp
- 宝宝嘻哈(有一段时间了居然忘记是什么了,不管,先记录再说啦!)
- CocoaPods详解之-Podfile.lock