标签:接下来 void ret 清零 name names family 数组越界 int
比如,S={1,2,2,2,3。5}。多重集S的众数是2,其重数为3。
对于给定的由n 个自然数组成的多重集S。计算S的众数及其重数。
输入
输入数据的第1行是多重集S中元素个数n(n<1300000);接下来的n行中,每行有一个最多含有5位数字的自然数,。
输出
输出数据的第1行给出众数,第2行是重数。
演示样例输入
6
1
2
2
2
3
5
演示样例输出
2
3
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
int n;
while(cin>>n)//数据的个数
{
int *array = new int [n+3];//多分配3个空间。避免数组越界
for(int i=0; i<n; i++)
cin>>array[i];//输入数据
sort(array,array+n);//升序排列
int index = 0,count_max = 1,count_temp = 1;//众数下标,重数,暂时重数
for(int i=1; i<n; i++)
if( array[i-1] != array[i])
{
if( count_max < count_temp )
{
count_max = count_temp;//记录重数
index = i-1;//记录众数的下标
}
count_temp = 1;//暂时重数清零
}
else
count_temp++;
if( count_max < count_temp )//若众数出如今最后面,则上面无法推断。所以要添加一次推断
{
count_max = count_temp;
index = n-1;//若众数出如今最后面,那么其下标就是n-1
}
cout<<array[index]<<endl<<count_max<<endl;
delete [] array;
}
return 0;
}
/**************************************
Problem id : SDUT OJ 1710
User name : 李俊
Result : Accepted
Take Memory : 5284K
Take Time : 480MS
Submit Time : 2014-04-23 00:12:07
**************************************/
标签:接下来 void ret 清零 name names family 数组越界 int
原文地址:http://www.cnblogs.com/yangykaifa/p/6746153.html