問題 E: 衆數
時間限制: 1 Sec 內存限制: 32 MB
題目描述
輸入20個數,每個數都在1-10之間,求1-10中的衆數(衆數就是出現次數最多的數,如果存在一樣多次數的衆數,則輸出權值較小的那一個)。
輸入
測試數據有多組,每組輸入20個1-10之間的數。
輸出
對於每組輸入,請輸出1-10中的衆數。
注意如果存在一樣多次數的衆數,則輸出權值較小的那一個。
樣例輸入
8 9 6 4 6 3 10 4 7 4 2 9 1 6 5 6 2 2 3 8
樣例輸出
6
經驗總結
基礎題~~
AC代碼
#include <cstdio>
#include <cstring>
int main()
{
int n;
while(~scanf("%d",&n))
{
int no[12]={0};
no[n]++;
for(int i=1;i<20;i++)
{
scanf("%d",&n);
no[n]++;
}
int max=1;
for(int i=2;i<=10;i++)
{
if(no[max]<no[i])
{
max=i;
}
}
printf("%d\n",max);
}
return 0;
}