PAT (Advanced Level) Practice 1054 The Dominant Color (20 分) 凌宸1642

PAT (Advanced Level) Practice 1054 The Dominant Color (20 分) 凌宸1642

题目描述:

Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800×600), you are supposed to point out the strictly dominant color.

译:在计算机内存的幕后,颜色通常被认为是每个像素由 24 位元组成的一系列信息。在一幅图像中,比例面积最大的颜色称为主色。严格的主色调占了总面积的一半以上。现在给出一个分辨率为 M × N 的图像(例如,800 × 600),你应该指出严格的主色调。


Input Specification (输入说明):

Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (≤ 800) and N (≤ 600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0,224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.

译:每个测试文件包含一个测试用例,第一行包含两个表示图片分辨率正整数 M (≤ 800) 和 N (≤ 600)。接下里 N 行中每行包含 M 个在 [ 0 , 224] 内表示颜色的数字 。题目保证对于每个输入的图片的严格的主色存在。在一行中的所有数字被空格分开。


Output Specification (输出说明):

For each test case, simply print the dominant color in a line.

译:对于每个测试用例,在一行中简单的输出主色 。


Sample Input (样例输入):

5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

Sample Output (样例输出):

24

The Idea:

由于需要统计输入的某个数字的次数,所以选择了 map ,但是需要的是 mapvalue 最大的元素,所以结合pairvectormap 进行 按照 value 进行 降序排列,所以第一个元素的 key 值就是我们需要的主色。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
map<int , int> mp ;
vector<pair<int , int> > ans ;
int m , n , t ;
bool cmp(pair<int , int> a , pair<int , int> b){
	return a.second > b.second ; // 按照 value 的降序排列
}
int main(){
	cin >> m >> n ;
	for(int i = 0 ; i < n ; i ++)
		for(int j = 0 ; j < m ; j ++){
			cin >> t ;
			mp[t] ++ ;
		}
	for(map<int , int>::iterator it = mp.begin() ; it != mp.end() ; it ++)
		ans.push_back(pair<int , int>(it -> first , it -> second)) ;//将 map中的元素转存至 vector
	sort(ans.begin() , ans.end() , cmp) ;
	cout << ans[0].first <<endl ;
	return 0 ;
}

posted @ 2021-04-08 20:19  凌宸1642  阅读(53)  评论(0编辑  收藏  举报