nyoj 众数问题
2014-12-29 15:47
253 查看
众数问题
时间限制:3000 ms | 内存限制:65535 KB难度:3
描述
所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数,
多重集合S重的重数最大的元素成为众数。例如:S={1,2,2,2,3,5},则多重集S的众数是2,其重数为3。
现在你的任务是:对于给定的由m个自然数组成的多重集S,计算出S的众数及其重数。
输入第一行为n,表示测试数据组数。(n<30)
每组测试的第一行是一个整数m,表示多重集S中元素的个数为m
接下来的一行中给出m(m<100)个不大于10万的自然数
(不会出现不同元素出现的次数相同的情况,如:S={11,11,22,22,33,33})。
输出每组测试数据输出一行,包含两个数,第一个是众数,第二个是其重数,中间以空格隔开。
样例输入
1 6 1 2 2 2 3 5
样例输出
2 3
对每一道题都要认真对待
#include<cstdio> #include<cstdlib> #include<cstring> #include<list> #include<algorithm> #include<functional> using namespace std; struct Node{ int max,count; }s; int main() { int t,m,a,i,n; while(scanf("%d",&n)==1){ while(n--){ scanf("%d",&m); list<int>l; for(i=0;i<m;++i){ scanf("%d",&a); l.push_back(a); } list<int>::iterator it =l.begin(); s.max=1;s.count=*it;t=1; l.sort(greater<int>()); l.push_back(-1); it=l.begin(); while(!l.empty()){ it++; if(l.front()!=*it){ if(t>s.max){ s.max=t; s.count=l.front(); } t=1; } else t++; l.pop_front(); if(l.front()==-1)break; } printf("%d %d\n",s.count,s.max); } } return 0; }
相关文章推荐
- NYOJ 95 众数问题
- nyoj 95 众数问题
- nyoj 95 众数问题【水】
- nyoj 95 众数问题
- nyoj 95 众数问题 【基础】
- NYOJ 95 众数问题(hash)
- NYOJ-95 众数问题(STL)
- nyoj95 众数问题(hash)
- nyoj 95 众数问题
- 【NYOJ 95】众数问题 &&STL map 学习
- nyoj 95 众数问题
- NYOJ-95-众数问题
- NYOJ 95 众数问题
- NYOJ 95 众数问题(map练习)
- NYOJ-众数问题
- nyoj-95-众数问题
- NYOJ 95 众数问题
- NYOJ95-众数问题
- nyoj--95--众数问题(水题)
- NYOJ 95 众数问题(map)