题目描述

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

输出例子:

24

版本一:

开始我以为主导颜色是指要连成一片的才算,零散分布不算,所以想了一个关于“岛问题”的解决方法,见版本二,后来才发现,原来就是数数,谁多,谁就是主导色
 #include <iostream>
#include <vector>
#include <map> using namespace std; int main()
{
int M,N;
int maxColor = ;
int resColor = ;
cin >> M >> N;
map<int, int>res;
vector<vector<int>>data(M, vector<int>(N, ));
for (int i = ; i < N; ++i)
{
for (int j = ; j < M; ++j)
{
int a;
cin >> a;
res[a]++;
}
}
for (auto ptr = res.begin(); ptr != res.end(); ++ptr)
{
if(ptr->second > maxColor)
{
maxColor = ptr->second;
resColor = ptr->first;
}
}
cout << resColor << endl;
return ; }

版本一:

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

版本三:

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

 #include <iostream>
#include <vector>
#include <unordered_map> using namespace std; int main()
{
int M, N;
cin >> M >> N;
unordered_map<int, int>res;
vector<vector<int>>data(M, vector<int>(N, ));
for (int i = ; i < N; ++i)
{
for (int j = ; j < M; ++j)
{
int a;
cin >> a;
res[a]++;
if (res[a] > M*N / )
{
cout << a << endl;
break;
}
}
}
return ; }
												

PAT甲级——【牛客A1005】的更多相关文章

  1. PAT甲级训练刷题代码记录

    刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...

  2. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  4. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  5. PAT——甲级1046S:shortest Distance

    这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N ex ...

  6. PAT甲级考前整理(2019年3月备考)之三,持续更新中.....

    PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...

  7. PAT甲级满分攻略|记一次考试经历

    一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...

  8. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  9. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

随机推荐

  1. 7.springboot+mybatis+redis整合

    选择生成的依赖 选择保存的工程路径 查询已经生成的依赖,并修改mysql的版本 <dependencies> <dependency> <groupId>org.s ...

  2. mysql UDF提权 sys_bineval

    介绍: 执行shellcode – sys_bineval sqmapl自带udf.dll中存在函数’sys_bineval,执行shellcode – sys_bineval 利用: MSF生成sh ...

  3. ASP.NET打开项目错误:将指定的计数添加到该信号量中会导致其超过最大计数。

    1.错误如图 2.解决方案 重启IIS即可,运行-> 输入IISRESET 命令 即可重启IIS,如图

  4. python_django_models模块中的查询

    查询集:表示从数据库获取的对象集合,查询集可以有多个过滤器,过滤器就是一个函数(方法),基于所给参数限制查询集结果 从sql角度来说,查询集和select等价,过滤器和where等价 查询集特点: 惰 ...

  5. Python自学:第五章 使用函数range( )

    # -*- coding: GBK -*- for value in range(1,5): print(value) 输出为: 1 2 3 4

  6. keep, preserve, noprune

    忘了紧急补充

  7. [JZOJ4616] 【NOI2016模拟7.12】二进制的世界

    题目 题目大意 给你一个数列,每个数为[0,65535][0,65535][0,65535]内的整数. 给定一个位运算操作optoptopt,是andandand.ororor.xorxorxor中的 ...

  8. 【JZOJ6368】质树(tree)

    description 大神 wyp 手里有棵二叉树,每个点有一个点权.大神 wyp 的这棵树是质树,因为 随便找两个不同的点 u, v,只要 u 是 v 的祖先,都满足 u 和 v 的点权互质. 现 ...

  9. 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗?

    本文为 Apache Flink 新版本重大功能特性解读之 Flink SQL 系列文章的开篇,Flink SQL 系列文章由其核心贡献者们分享,涵盖基础知识.实践.调优.内部实现等各个方面,带你由浅 ...

  10. luoguP1029 最大公约数和最小公倍数问题 [gcd][数论]

    题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P,Q是正整数 2.要求P,Q以x0为 ...