AOJ - Problem 1008 : What Color Is The Universe?
A = {a[1] , a[2] , ... a[n] }について
回数が|A|/2より多く出現する数字を出力する問題です。(|A|は要素数)
#include <iostream> #include <map> using namespace std; int main(){ int n, a; while( cin >> n , n ){ map<int,int> m; for(int i=0 ; i < n ; i++ ){ cin >> a; if( m.count(a) ){ m[a] += 1; }else{ m[a] = 1; } } int ans=-1; for(map<int,int>::iterator it = m.begin() ; it != m.end() ; ++it ){ if( it->second > n/2 ){ ans = it->first; } } if( ans == -1 ){ cout << "NO COLOR" << endl; }else{ cout << ans << endl; } } }