BestCoder Round #50 (div.2) & HDOJ5364 Search Result(模拟)

本文探讨了一种解决特定问题的模拟算法,通过输入不同数量的整数和对应的ID,输出需要被惩罚的人的ID。当没有人需要被惩罚时,输出-1。详细解释了算法的实现过程和关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input
There are multiply cases.
For each case,there is a single integer n(1<=n<=1000) in first line.
In second line,there are n integer a1,a2...an(0<=ai<10000)ai is the the ith man's ID.
 

Output
Output ID of the man who should be punished.
If nobody should be punished,output -1.
 

Sample Input
3 1 1 2 4 2 1 4 3
 

Sample Output
1 -1
 


简单的小模拟(竟然wa了。。读题啊!)


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10005;
int n, a[maxn];
int main()
{
    while(scanf("%d", &n) != EOF) {
        memset(a, 0, sizeof(a));
        for(int i = 0; i < n; ++i) {
            int x;
            scanf("%d", &x);
            a[x]++;
        }
        int m = 0, flag, sum = 0;
        for(int i = 0; i < maxn; ++i) {
            if(a[i] > m) {
                m = a[i];
                flag = i;
            }
        }
        if(m == 1) {
            printf("-1\n");
            continue;
        }
        for(int i = 0; i < maxn; ++i)
            if(i != flag)
                sum += a[i];
        if(m > sum) printf("%d\n", flag);
        else printf("-1\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值