個人部落格:doubleq.win
090 衆數
時間限制: 1 s
空間限制: 1000 KB
題目等級 : 青銅 Bronze
題解
題目描述 Description
由檔案給出N個1到30000間無序數正整數,其中1≤N≤10000,同一個正整數可能會出現多次,出現次數最多的整數稱為衆數。求出它的衆數及它出現的次數。
輸入描述 Input Description
輸入檔案第一行是正整數的個數N,第二行開始為N個正整數。
輸出描述 Output Description
輸出檔案有若幹行,每行兩個數,第1個是衆數,第2個是衆數出現的次數。
樣例輸入 Sample Input
12
2 4 2 3 2 5 3 7 2 3 4 3
樣例輸出 Sample Output
2 4
3 4
資料範圍及提示 Data Size & Hint
1 #include<iostream>
2 using namespace std;
3 int a[100001];
4 int tot=0;
5 int b[100001];
6 int maxn;
7 int ma;
8 int main()
9 {
10 int n;
11 cin>>n;
12 for(int i=1;i<=n;i++)
13 {
14 cin>>a[i];
15 b[a[i]]++;
16 if(b[a[i]]>maxn)
17 maxn=b[a[i]];
18 if(a[i]>ma)
19 ma=a[i];
20 }
21 for(int i=0;i<=ma;i++)
22 {
23 if(b[i]==maxn)
24 {
25 cout<<i<<" "<<b[i]<<endl;
26 }
27 }
28 return 0;
29 }