A. Elections
2015-08-14 19:50
197 查看
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities.
We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the
maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one
with a smaller index.
Determine who will win the elections.
Input
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 100)
— the number of candidates and of cities, respectively.
Each of the next m lines contains n non-negative
integers, the j-th number in the i-th
line aij (1 ≤ j ≤ n, 1 ≤ i ≤ m,0 ≤ aij ≤ 109)
denotes the number of votes for candidate j in city i.
It is guaranteed that the total number of people in all the cities does not exceed 109.
Output
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.
Sample test(s)
input
output
input
output
Note
Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.
Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that
everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
解题说明:此题是一道模拟题,按照题意要求进行统计即可。
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[105];
int main()
{
int z,j,n,m,max,ind,i;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
max=-999;
for(j=1;j<=n;j++)
{
scanf("%d",&z);
if(z>max)
{
max=z;
ind=j;
}
}
a[ind]++;
}
max=-999;
for(i=1;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
ind=i;
}
}
printf("%d\n",ind);
return 0;
}
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities.
We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the
maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one
with a smaller index.
Determine who will win the elections.
Input
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 100)
— the number of candidates and of cities, respectively.
Each of the next m lines contains n non-negative
integers, the j-th number in the i-th
line aij (1 ≤ j ≤ n, 1 ≤ i ≤ m,0 ≤ aij ≤ 109)
denotes the number of votes for candidate j in city i.
It is guaranteed that the total number of people in all the cities does not exceed 109.
Output
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.
Sample test(s)
input
3 3 1 2 3 2 3 1 1 2 1
output
2
input
3 4 10 10 3 5 1 6 2 2 2 1 5 7
output
1
Note
Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.
Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that
everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
解题说明:此题是一道模拟题,按照题意要求进行统计即可。
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[105];
int main()
{
int z,j,n,m,max,ind,i;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
max=-999;
for(j=1;j<=n;j++)
{
scanf("%d",&z);
if(z>max)
{
max=z;
ind=j;
}
}
a[ind]++;
}
max=-999;
for(i=1;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
ind=i;
}
}
printf("%d\n",ind);
return 0;
}
相关文章推荐
- 小白学开发(iOS)OC_基本数据类型转换成字符串(2015-08-12)
- OC - NSDictionaryAndNSMutableDictionary
- 黑马程序员-Scanner的使用、string类的概述和使用
- Hadoop之SequenceFile
- Performing User-Managed Database-18.4、Restoring Datafiles and Archived Redo Logs
- c++最简单的静态链接库创建
- 并查集详解 (转)
- RHEL6.5上Oracle ACFS与Linux samba一起使用时遇到的bug
- 详解Hibernate中的一级缓存
- ZedBoard的简单测试
- JDK各个版本的新特性jdk1.5-jdk8
- 小白学开发(iOS)OC_ 字符串的比较判断(2015-08-12)
- leetcode 138 —— Copy List with Random Pointer
- 常用Oracle数据表操作命令
- 解决 Linux Mint 17.2 Nvidia驱动下屏幕亮度无法调节问题
- curl获取图片
- 观察者模式
- 机会—晋升—改变未来
- Python爬虫之<单线程爬虫>
- 单例模式