1. 程式人生 > >A1054 The Dominant Color

A1054 The Dominant Color

題目很簡單,用map省空間,如果直接開很大的陣列統計數字出現次數,不知道會不會爆記憶體,畢竟數字到了2^24.
沒必要開二維陣列,反正空間都是m*n。

#include <stdio.h>
#include <map>
using namespace std;

int main(int argc, char const *argv[])
{
	int m,n;
	scanf("%d%d",&m,&n);
	int color;
	map<int,int> mp;
	for(int i=0;i<m*n;i++){
		scanf
("%d",&color); mp[color]++; } for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){ if(it->second > m*n/2){ printf("%d",it->first); break; } } return 0; }