AOJ - Problem 0028 : Mode Value
入力された整数値のうち出現頻度が多いものを出力する問題です。
数字ごとに何回入力されたかを配列などに保持して最後にその出現回数の最大のものを出力するようにします。
複数あるときは、昇順にソートしてから複数の数字を出力します。
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; int main(){ int n, m=0; map<int,int> a; vector<int> ans; for(int i=1 ; i<=100 ; i++){ a.insert( map<int,int>::value_type( i , 0 ) ); } while( cin >> n ){ a[n] += 1; } for(int i=1 ; i<=100 ; i++){ m = max( a[i] , m ); } for(int i=1 ; i<=100 ; i++){ if( m == a[i] ) ans.push_back(i); } sort( ans.begin() , ans.end() ); for(int i=0 ; i<ans.size() ; i++){ cout << ans[i] << endl; } }