其他分享
首页 > 其他分享> > 1054 The Dominant Color (20 分)

1054 The Dominant Color (20 分)

作者:互联网

1. 题目

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.

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.

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

2. 题意

给出m×n大小的屏幕,每个点位会有一个颜色值,找出整个屏幕中颜色值占最多那个颜色。

3. 思路——map的使用

创建一个map,键值对为<颜色, 数量>,对屏幕中的所有颜色进行计数,计数完成后,遍历找出数量最多的那个颜色。

4. 代码

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int m, n;
	cin >> m >> n;
	map<int, int> res;
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int x;
			cin >> x;
			res[x] += 1;
		}
	}
	int dominantColor = 0;
	int maxCnt = 0;
	// map的遍历
	// 如果有新颜色在屏幕中出现次数大于当前记录的颜色,则更新 
	for (map<int, int>::iterator iter = res.begin(); iter != res.end(); iter++)
		if (iter->second > maxCnt) maxCnt = iter->second, dominantColor = iter->first; 
	
	cout << dominantColor << endl;
	return 0;
} 

标签:24,1054,Color,dominant,iter,int,each,Dominant,color
来源: https://www.cnblogs.com/vanishzeng/p/15479930.html