CCF篇:2013-12-1:出現次數最多的數
阿新 • • 發佈:2018-11-10
問題描述
給定n個正整數,找出它們中出現次數最多的數。如果這樣的數有多個,請輸出其中最小的一個。
輸入格式
輸入的第一行只有一個正整數n(1 ≤ n ≤ 1000),表示數字的個數。
輸入的第二行有n個整數s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相鄰的數用空格分隔。
輸出格式
輸出這n個次數中出現次數最多的數。如果這樣的數有多個,輸出其中最小的一個。
樣例輸入
6
10 1 10 20 30 20
樣例輸出
10
解決:
#include<iostream> #include<cstdio> using namespace std; int main(){ int n; int a[n],b[n],arry_count[n]; int max=0; int v_min=10001; //得到n,a[i] scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int j=0;j<n;j++){ b[j]=a[j]; } for(int i=0;i<n;i++){ //得到計數陣列arry_count int temp=a[i]; int count=0; for(int j=0;j<n;j++){ if(a[j]==temp) count++; } arry_count[i]=count; } for(int j=0;j<n;j++){ //得到最大值max if(max<arry_count[j]){ max=arry_count[j]; } } for(int k=0;k<n;k++){ if(arry_count[k]==max){ int temp = a[k]; if(temp<v_min) v_min = temp; } } printf("%d",v_min); return 0; }