您的位置:首页 > 理论基础

1075:众数问题

2017-09-18 12:07 183 查看
1075:众数问题

Description

问题描述:

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

例如,S={1,2,2,2,3,5}。多重集S的众数是2,其重数为3。

编程任务:

对于给定的由n 个自然数组成的多重集S,编程计算S 的众数及其重数。

Input

第1行多重集S中元素个数n(n<=50000);接下来的n 行中,每行有一个自然数。

Output

输出文件有2 行,第1 行给出众数,第2 行是重数。(如果有多个众数,只输出最小的)

Sample Input

6
1
2
2
2
3
5

Sample Output

2
3

#include<iostream>
using namespace std;
int main()
{
int n,a[1000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int max,count=0,temp;
for(int i=0;i<n-1;i++)
{
if(a[i]==-1)
{
continue;
}
temp=1;
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
a[j]=-1;
temp++;
}
}
if(temp>count||temp==count&&a[i]<max)
{
max=a[i];
count=temp;
}
}
cout<<max<<endl;
cout<<count<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息