博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——【牛客A1005】
阅读量:4541 次
发布时间:2019-06-08

本文共 3369 字,大约阅读时间需要 11 分钟。

题目描述

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 30 0 255 16777215 2424 24 0 0 2424 0 24 24 24

 

输出例子:

24

版本一:

开始我以为主导颜色是指要连成一片的才算,零散分布不算,所以想了一个关于“岛问题”的解决方法,见版本二,后来才发现,原来就是数数,谁多,谁就是主导色
1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 int main() 8 { 9 int M,N;10 int maxColor = 0;11 int resColor = 0;12 cin >> M >> N;13 map
res;14 vector
>data(M, vector
(N, 0));15 for (int i = 0; i < N; ++i)16 {17 for (int j = 0; j < M; ++j)18 {19 int a;20 cin >> a;21 res[a]++; 22 }23 }24 for (auto ptr = res.begin(); ptr != res.end(); ++ptr)25 {26 if(ptr->second > maxColor)27 {28 maxColor = ptr->second;29 resColor = ptr->first;30 }31 }32 cout << resColor << endl;33 return 0;34 35 }

 

版本一:

关于“岛问题”,使用递归遍历,就是对于每一种颜色,通过上下左右遍历出其所有的相同的颜色,并计数和标记,则得到每种颜色最多的主导色
#include 
#include
#include
#include
using namespace std;int M, N;//方法一,使用void Travle(vector
>&data, int a, int b, int &num, const int color){ if ( a < 0 || b < 0 || a >= M || b >= N|| data[a][b] != color) return; num++; data[a][b] = -1;//标记已遍历 Travle(data, a - 1, b, num, color); Travle(data, a + 1, b, num, color); Travle(data, a, b - 1, num, color); Travle(data, a, b + 1, num, color);}int main(){ Test1(); //M*N int maxColor = 0; int resColor = 0; cin >> M >> N; vector
>data(M, vector
(N, 0)); for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) cin >> data[j][i]; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { if (data[i][j] >= 0)//未遍历过 { int num = 1; int color = data[i][j]; Travle(data, i, j, num, color); if (num > maxColor) { maxColor = num; resColor = color; } } } } cout << resColor << endl; return 0;}

版本三:

  笨死了,英语理解能力不好,其实就是监测每次的输入,当输入某种颜色的个数过半,则立马输出!

1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 int main() 8 { 9 int M, N;10 cin >> M >> N;11 unordered_map
res;12 vector
>data(M, vector
(N, 0));13 for (int i = 0; i < N; ++i)14 {15 for (int j = 0; j < M; ++j)16 {17 int a;18 cin >> a;19 res[a]++;20 if (res[a] > M*N / 2)21 {22 cout << a << endl;23 break;24 }25 }26 } 27 return 0;28 29 }

 

 

转载于:https://www.cnblogs.com/zzw1024/p/11158723.html

你可能感兴趣的文章
Django Form 的主要内置字段介绍
查看>>
如何写好一个UITableView
查看>>
XML文件生成C++代码(基于rapidxml)
查看>>
写代码,更需要设计代码
查看>>
iOS:修改项目名
查看>>
SpringCloud-Eureka
查看>>
double在输出为字符串的几种方法效率测试
查看>>
ArcGIS API for JavaScript 4.2学习笔记[14] 弹窗的位置、为弹窗添加元素
查看>>
电路基础
查看>>
jquery 对象与DOM对象转换
查看>>
DELPHI 调用系统 ADO 配置窗体 提高软件易用性
查看>>
Mongodb 命令及 PyMongo 库的使用
查看>>
div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(非原创)
查看>>
关于SDWebImage加载高清图片导致app崩溃的问题
查看>>
如何查看方法在哪里被调用
查看>>
HUE的自动化安装部署
查看>>
图片服务器(FastDFS)的搭建
查看>>
myBatis应用
查看>>
RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
查看>>
[PHP] 用AppServ一步到位安装PHP服务器
查看>>