Popular Vote

in an election with more than two candidates, it is often the case that the winner (the candidate receiving the most votes) receives less than the majority of the votes. 
Given the results of an election, can you determine the winner, and whether the winner received more than half of the votes?

输入

The first line of input contains a single positive integer T ≤ 500 indicating the number of test cases. The first line of each test case also contains a single positive integer n indicating the number of candidates in the election. This is followed by n lines, with the ith line containing a single nonnegative integer indicating the number of votes candidate i received.
There are at least 2 and no more than 10 candidates in each case, and each candidate will not receive more than 50 000 votes. There will be at least one vote cast in each election.

输出

Provide a line of output for each test case. If the winner receives more than half of the votes, print the phrase majority winner followed by the candidate number of the winner. If the winner does not receive more than half of the votes, print the phrase minority winner followed by the candidate number of the winner. If a winner cannot be determined because no single candidate has more vote than others, print the phrase no winner. The candidate numbers in each case are 1, 2, . . . , n.

样例输入 Copy

4
3
10
21
10
3
20
10
10
3
10
10
10
4
15
15
15
45

样例输出 Copy

majority winner 2
minority winner 1
no winner
minority winner 4

解决思路:找出最大的票数,并且找出序号,在看看最大值存在几个;

代码展示:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int l;
    int *a=new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>l;
        int *b=new int[l];
        int max=0;
        int w=0;
        for(int j=0;j<l;j++)
        {
            cin>>b[j];
            if(b[j]>max)
            {
                max=b[j];
                w=j+1;
            }
        }
        sort(b,b+l); 
        if(b[l-1]==b[l-2])a[i]=0;
        else a[i]=w;
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]==0)cout<<"no winner"<<endl;
        else cout<<"minority winner"<<a[i]<<endl;
    } 
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值