题目描述
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, 800x600), you are supposed to point out the strictly dominant color.
输入描述:
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.
输出描述:
For each test case, simply print the dominant color in a line.
输入例子:
5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24
思路
题意就是要找到出现次数超过整个矩阵点数总数一半的那个数,因为题目表明必定存在,那么找出出现次数最多的那个数字就是最终解。
一开始想的还是双重循环读取数字,额,果然是我太嫩。
读取的时候还是要像这样读可以更好的找出最终解,那么就转换成CCF里的出现最多次数那一个题了,然后就顺理成章地解了,详见法二。
法一:
#include<iostream>
using namespace std;
int main()
{
int M,N,i,j;
int x;
int times=0;
int result;
cin>>M>>N;
for(i=0;i<M*N;i++)
{
cin>>x;
if(times==0)
result=x;
if(x==result)
times++;
else
times--;
}
cout<<result<<endl;
return 0;
}
法二:
数组越界了...看看就行...算了忽略吧,但是算自己的一个学习过程吧。
#include<iostream>
#define N 480001
using namespace std;
int a[N] = {0};
int main()
{
int m, n;
cin >> m >> n;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
int t;
cin >> t;
a[t]++;
if (a[t] > m*n/2)
{
cout << t;
break;
}
}
//break;
}
return 0;
}