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的24次方16777216是比600*800=480000来的数量极高,所以采用记录每个数字的方式及其出现次数的方式。
#include<iostream>
using namespace std;
struct Number {
long num;
long c;
};
int main() {
int n, m;
long count=0,t,max=0,k;
Number a[480001];
cin >> n >> m;
for (long i = 0; i < n*m; i++) {
cin >> t;
bool f=false;
for (long j = 0; j < count; j++) {
if (t == a[j].num) {
a[j].c++;
f = true;
break;
}
}
if (f == false) {
a[count].num = t;
a[count].c = 1;
count++;
}
}
for (long i = 0; i < count; i++) {
if (a[i].c > max) {
k = a[i].num;
max = a[i].c;
}
}
cout << k;
return 0;
}
欢迎指导和交流!