求众数

#include <stdio.h>
int num=0;    //存放众数
int maxcount=0;      //存放重数
void split(int a[],int low,int high,int &mid,int &left,int &right){
    mid=(low+high)/2;
    for (left=mid;left<=high;left--)
    {
        if(a[left]!=a[mid]){
             break;
        }    
    }
     for (right=mid;right>=low;right++)
        if(a[right]!=a[mid])
             break;      
   
}
void getmaxcount(int a[],int low,int high){
    if(low<=high){
        int mid,left,right;
        split(a,low,high,mid,left,right);
        int cnt=right-left-1;
        if(cnt>maxcount){
            num=a[mid];
            maxcount=cnt;
        }
        getmaxcount(a,low,left);
        getmaxcount(a,right,high);
    }
}
int main(){
    int a[]={1,2,2,2,3,5,6,6,6,6};
    getmaxcount(a,0,9);
    printf("众数%d\n重数%d",num,maxcount);
    return 0;
}
posted @ 2023-04-11 15:34  Dog17  阅读(21)  评论(0)    收藏  举报