您的位置:首页 > 理论基础 > 计算机网络

Problem 1582 众数问题 from http://acm.fzu.edu.cn/problem.php?pid=1582

2012-10-27 01:18 411 查看




Problem
Description

给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为众数。

例如,S={1,2,2,2,3,5}。

多重集S的众数是2,其重数为3。




Input

输入包括多组数据,请处理到EOF结束。

每组数据,以一个n(1<=n<=100,000)开始,接下n行,每行有一个数字(-231~231)。




Output

对于每组输入数据,输出一行一个数字,表示众数。如果存在多个解,只需输出值最小的众数即可。




Sample
Input

61222353-1-1-1




Sample
Output

2-1




Source

FOJ月赛-2008年4月

这种思路还可以应用在求最小差上面

[cpp] view
plaincopy

#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<map>

using namespace std;

int a[100000];

int main(){

int n, t, max, val;

//freopen("in.txt", "r", stdin);

while(scanf("%d", &n)!=EOF){

for(int i=0;i<n;++i)

scanf("%d", a+i);

sort(a, a+n);

int pre = a[0];

int count = 1, max = 1, val=pre;

for(int i=1;i<n;++i){

if(a[i] == pre)

count++;

else{

pre = a[i];

count = 1;

}

if(max<count){

max = count;

val = a[i];

}

}

printf("%d\n", val);

}

//fclose(stdin);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: