PAT甲级1054 The Dominant Color (20分)|C++实现

一、题目描述

原题链接
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:

在这里插入图片描述

​​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

二、解题思路

20分简单题,统计各个数字出现的次数即可,我这里用了map,事实上用简单数组也可以实现。遍历map,找出比当前最大值大的即可。

三、AC代码

#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
map<long long, int> mp;
int main()
{
    
    
    int M, N;
    long long tmp, most = -1;
    int cntMax = 0;
    scanf("%d%d", &M, &N);
    for(int i=0; i<N; i++)
    {
    
    
        for(int j=0; j<M; j++)
        {
    
    
            scanf("%d", &tmp);
            mp[tmp]++;
        }
    }
    for(auto it : mp)
    {
    
    
        if(it.second > cntMax)
        {
    
    
            cntMax = it.second;
            most = it.first;
        }
    }
    printf("%d", most);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/108614389