题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768
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
:题意 找到n行m列中的严格主导数字(“严格主导”:超过总数的一半)
将比较大的数组定义在main()外部
数组定义在函数中时,占用的内存来自栈空间,局部变量是在栈上分配内存空间的,栈空间是在进程创建时初始化的,有固定的大小,一般为几十KB,所以太大的数组会耗光栈空间,会造成栈溢出。 全局变量一般分配在数据段,全局变量在静态存储区分配内存,可以比较大。
#include<iostream>
#include<cstring>
using namespace std;
int a[100000001];
int main(){
int n,m,x;
bool f=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&x);
a[x]++;
if(a[x]>m*n/2){
printf("%d\n",x);
f=1;
break;
}
}
if(f==1){
break;
}
}
return 0;
}