当前位置:网站首页>PAT 1054 The Dominant Color
PAT 1054 The Dominant Color
2022-06-13 04:12:00 【Zhemu】
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:
24The question : Give a 5*3 Matrix , Find the number in the matrix that is more than half of the number in the matrix and appears the most times , namely 24;
Ideas :
Because the input data (16777215,1*10^7) Greater than int Maximum range ( The maximum range of integers is 8388608, namely 8*10^6), So you can't use a hash table of arrays here , Should use the map As hash table , stay map of use find function , If you can find it, you can count ++, If it cannot be found, it means that this number does not exist before , Set as 1, Last traversal map, Just find the output with the most times
Study :
1.map There is no need to open the size range for the definition of
2.map The traversal ,for(map< type , type >::iterator it=map.begin();it!=map.end();it++)
3.map Of key and value The interview of ,key:it->first,value:it->second;
Code :
//map Use ;
// The title says that the maximum range of input numbers is to 2^24, Already exceeded int The maximum range of , Opening a hash table so large may cause hyperspace , So here is map;
#include<iostream>
#include<map>
using namespace std;
int main(){
int n,m,num;
scanf("%d %d",&n,&m);
map<int ,int > count;
for(int i=0;i<n;i++){// Don't use it directly n*m, Multiplication of two integer numbers is easy to overflow ;
for(int j=0;j<m;j++){
scanf("%d",&num);
if(count.find(num)!=count.end()){// If present, count , If not, create and set to 1;
count[num]++;
}else {
count[num]=1;
}
}
}
int max=-1;
for(map<int,int>::iterator it=count.begin();it!=count.end();it++){// Iterative way
//for( type ::iterator it= type .begin(),it!= type .end();it++)
if(it->second>max){
max=it->second;//it It can represent map An item of ;
num=it->first;
}
}
printf("%d",num);
return 0;
}边栏推荐
- Lambda termination operation find and match nonematch
- Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
- 单片机:Modbus 多机通信程序设计
- MCU: EEPROM multi byte read / write operation sequence
- Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
- Dumi builds a document blog
- Interpretation of mobile phone private charging protocol
- 【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
- Lambda termination operation find and match anymatch
- ROS topics and nodes
猜你喜欢

10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects

Alipay native components (hotel time selection)

重读经典:《End-to-End Object Detection with Transformers》

Interpretation of mobile phone private charging protocol

R: Employee turnover forecast practice
![[notes] summarize common horizontal and vertical centering methods](/img/0a/9625e7e459be69a2a8d4e016a20f4d.jpg)
[notes] summarize common horizontal and vertical centering methods

单片机:RS485 通信与 Modbus 协议

knife4j aggregation 2.0.9支持路由文档自动刷新

ET框架-22 创建ServerInfo实体及事件

Call C function in Lua
随机推荐
Dumi construit un blog documentaire
Forgotten fleeting years
手机私有充电协议解读
单片机:PCF8591 应用程序
Goframe day 4
Common ways to traverse map sets
Sword finger offer II 022 Entry node of a link in a linked list
Discussion sur la modélisation de la série 143
[test development] use case
单片机:红外遥控通信原理
EIA map making - data processing + map making
微信扫描二维码无法下载文件的解决办法
Single chip microcomputer: MODBUS multi computer communication program design
史上最详细的Swin-Transformer 掩码机制(mask of window attentation)————shaoshuai
Among the four common technologies for UAV obstacle avoidance, why does Dajiang prefer binocular vision
高等数学(第七版)同济大学 习题1-2 个人解答
Lambda termination operation find and match nonematch
[test development] file compression project practice
Cache read / write -- write
单片机:A/D 和 D/A 的基本概念