当前位置:网站首页>7-7 solving mode problems

7-7 solving mode problems

2022-06-24 23:31:00 White -

7-7 Solve the mode problem

Given to contain n Multiple sets of elements S, Each element in S The number of occurrences in is called the multiplicity of the element . Multiple sets S The element with the largest multiplicity is called mode . for example ,S={1,2,2,2,3,5}. Multiple sets S What is the mode 2, Its multiplicity is 3. For a given by n A multiple set of natural numbers S, Calculation S Mode and multiplicity of . If there are multiple modes , Please output the smallest one .

Input format :
Number of input data 1 Rows are multiple sets S The number of elements in n(n<1000); Second line input n Up to 5 A natural number of digits .

14
1 2 2 2 3 3 5 6 6 5 6 2 7 6

Output format :
The second of the output data 1 OK, give me a few , The first 2 A row is a multiplicity .

2 4

Code :

#include <stdio.h>
#include <stdlib.h>
int n;
int a[10100];
int temp[10100];
int min=9999;
int mintime=0;
int main()
{
    
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
    {
    
        int x=a[i];
        temp[x]++;
        if(temp[x]>mintime)
        {
    
            mintime=temp[x];
            min=x;
        }
        else if(temp[x]==mintime&&x<min)
            min=x;
    }
    printf("%d %d",min,mintime);
    return 0;
}

202206222116 3、 ... and

原网站

版权声明
本文为[White -]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241820370561.html