众数

#include<iostream>
using namespace std;
template <class T>
T mass(T *a, int num)
{
    int *b=new int[num];
    for(int kp=0;kp<num;kp++)
        b[kp]=0;
    for(int i=0;i<num;i++)
        for(int j=0;j<num;j++)
        {
            if(a[i]==a[j])
                b[i]+=1;
        }
        int m=0,n=0,temp=b[0];
        for(int k=0;k<num;k++)
        {
            if(temp<b[k])
            {
                temp=b[k];
                m=k;
            }
        }
        if(temp==1) return -1;//没有众数
        return a[m];
}
void main()
{
    double  a=0;
    int size = 3;
    double *b=new double [size];
    b[0]=0;
    b[1]=1.2;
    b[2]=1.2;
    a=mass(b,3);
    delete[] b ;
    cout<<a<<endl;
    cin>>a;
}
原文地址:https://www.cnblogs.com/lwngreat/p/4555402.html