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

 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 plain copy
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<algorithm>  
  4. #include<cstring>  
  5. #include<map>  
  6. using namespace std;  
  7. int a[100000];  
  8. int main(){  
  9.     int n, t, max, val;  
  10.     //freopen("in.txt", "r", stdin);  
  11.     while(scanf("%d", &n)!=EOF){  
  12.   
  13.         for(int i=0;i<n;++i)  
  14.             scanf("%d", a+i);  
  15.         sort(a, a+n);  
  16.         int pre = a[0];  
  17.         int count = 1, max = 1, val=pre;  
  18.         for(int i=1;i<n;++i){  
  19.             if(a[i] == pre)  
  20.                 count++;  
  21.             else{  
  22.                 pre = a[i];  
  23.                 count = 1;  
  24.             }  
  25.             if(max<count){  
  26.                 max = count;  
  27.                 val = a[i];  
  28.             }  
  29.         }  
  30.         printf("%d\n", val);  
  31.   
  32.     }  
  33.   
  34.    //fclose(stdin);  
  35.   
  36. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值