1. 程式人生 > >C語言訓練-3740-眾數

C語言訓練-3740-眾數

Problem Description
眾數是指在一組資料中,出現次數最多的數。例如:1, 1, 3 中出現次數最多的數為 1,則眾數為 1。
給定一組數,你能求出眾數嗎?
Input
輸入資料有多組(資料組數不超過 50),到 EOF 結束。
對於每組資料:
•第 1 行輸入一個整數 n (1 <= n <= 10000),表示數的個數。
•第 2 行輸入 n 個用空格隔開的整數 Ai (0 <= Ai <= 1000),依次表示每一個數。
Output
對於每組資料,在一行中輸出一個整數,表示這組資料的眾數。
資料保證有唯一的眾數。
Example Input
3
1 1 3
5
0 2 3 1 2
Example Output


1
2

#include<stdio.h>
int main(int argc, char const *argv[])
{
int n,t,max,A,f,i,m=50;
while (~scanf("%d",&n)&&m)
{
int a[1005]={-1};
f = 0;
for(i=0;i<n;i++)
{
  scanf("%d",&A);
  a[A]++;
  if(f<A){
  f = A;}
}
max = -1;
for (i = 0;i <= f; i++)
{
  if(a[i] > max)
  {  max = a[i];
    t = i;}
}
printf("%d\n", t);
m--;
}
  return 0;
}