zoukankan      html  css  js  c++  java
  • 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;
    } 
    
  • 相关阅读:
    怎样使用jlink一键烧录整个flash Hi3518 a c e Hi3515 Hi3512
    阅读zepto.js的core中的Core methods
    linux下执行strlwr函数出错:ld returned 1 exit status
    POJ 3286 How many 0&#39;s?
    As long as Binbin loves Sangsang
    docker 的安装
    浅析JAVA设计模式之工厂模式(一)
    怎样在屏幕上显示多个alv
    链表的应用:单元多项式的加法、减法、乘法
    Android 开发中 iBeacon的使用
  • 原文地址:https://www.cnblogs.com/vanishzeng/p/15479930.html
Copyright © 2011-2022 走看看