C語言編程練習——出現次數最多的數

/*問題描述:出現次數最多的數code

  給定n個正整數,找出它們中出現次數最多的數。若是這樣的數有多個,請輸出其中最小的一個。io

輸入格式class

  輸入的第一行只有一個正整數n(1 ≤ n ≤ 1000),表示數字的個數。di

  輸入的第二行有n個整數s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相鄰的數用空格分隔。co

輸出格式數字

  輸出這n個次數中出現次數最多的數。若是這樣的數有多個,輸出其中最小的一個。return

樣例輸入printf

6

10 1 10 20 30 20

樣例輸出

10*/

#include <stdio.h>
#include <stdlib.h> 
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n-1];
    int ay[10000] = {0};
    int temp = 0;
    int i = 0,j;
    int number1 = 0;
    for(i = 0;i < n;i ++)
    {
        scanf("%d",&arr[i]);
    }
    int *amax = (int*)malloc(sizeof(int));
    *amax = arr[0];
    for(i = 0;i < n;i ++)
    {
        if(*amax < arr[i])
            *amax = arr[i];
    }
    for (i = 0; i< n;i ++)
    {
        temp = arr[i];
        ay[temp] ++;
        }
        int *max = (int*)malloc(sizeof(int));        
        *max = ay[0];
       
    for(j = 0;j <= *amax;j ++)
    {
        if(*max < ay[j])
           {
               *max = ay[j];
               number1=j;
           }
 
    }
    printf("%d\n",number1);
    return 0;
}
相關文章
相關標籤/搜索