A1054 The Dominant Color (20 point(s))

记录数组出现个数,取超过一半的数

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:

1
2
3
4
5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

Sample output:

1
24

2. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<cstdio>
#include<map>
using namespace std;
map<int,int> match;
int main()
{
int m,n;
scanf("%d%d",&m,&n);
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
int a;
scanf("%d",&a);
match[a]++;
}
}
for (map<int,int>::iterator i = match.begin(); i != match.end(); ++i)
{
if (i->second>(m*n/2))
{
printf("%d\n", i->first);
break;
}
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1054/
  • 发布时间: 2019-09-03 12:33
  • 更新时间: 2021-10-29 14:02
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

Related Issues not found

Please contact @WTlumos to initialize the comment