PAT A1054 The Dominant Color

WAY1:

#include
#include
#include
using namespace std;
const int maxn = pow(2,24);
int arr[16777216]={0}; 
int main(){
	int x,y;
	int pixel;
	scanf("%d %d",&x,&y);
	for(int i = 0; i < y; i++){
		for(int j=0;j<x;j++){
			scanf("%d",&pixel);
			arr[pixel] += 1; 
		}
	}
	int temp = 0,index = 0;
	for(int i =0;i<16777216;i++){
		if(arr[i]>temp){
		  temp = arr[i];
		  index = i;
		} 

	}
	printf("%d",index);
	
}

WAY2;

#include
#include
#include
#include
using namespace std;
map<int,int> pixel;
int main(){
	int x,y,value;
	scanf("%d %d",&x,&y);
	for(int i = 0; i< x; i++){
		for(int j=0;j < y;j++){
			scanf("%d",&value);
			if(pixel.find(value) != pixel.end()) pixel[value]++;
			else pixel[value] = 1;
		}
	}
	int k = 0, max = 0;
	for(map<int,int>::iterator mp = pixel.begin();mp!=pixel.end();mp++){
		if (mp->second >  max) {
			k = mp->first;
			max = mp->second;
		}
	}
	printf("%d",k);
	return 0;
}

你可能感兴趣的:(PAT A1054 The Dominant Color)