PAT甲级——【牛客A1005】
题目描述
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】的更多相关文章
- PAT甲级训练刷题代码记录
刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- 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 ...
- PAT——甲级1046S:shortest Distance
这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N ex ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
随机推荐
- hive sparksession查询只显示defalt库问题
1.spark环境记得拷贝进hive.xml 2.SparkSession.builder().enableHiveSupport()记得加上enableHiveSupport 3.window记得w ...
- eclipse导出说明文档
选中项目--右键--Export--Java--Javadoc—Finish 1.为程序添加文档注释 2.选中项目--右键Export--Java--Javadoc--next, 3.next--在V ...
- iptbales无法正常重启
新主机iptables无法启动关闭和重启 一般是由于没有配文件导致 解决办法 直接touch /etc/sysconfig/iptables 然后就可以正常启动. 备注:一般存在于centos6系列中
- Spring Cloud失散多年的哥哥Dubbo学习笔记
Spring Cloud失散多年的哥哥Dubbo 随着互联网项目用户量的急剧增长,访问并发良突然暴增,将一个应用使用多个独立的工程共同实现的系统架构,称为SOA系统架构,各个工程可以允许在不同的机器上 ...
- Ubuntu 更新错误修复大全
合并列表问题 当你在终端中运行更新命令时,你可能会碰到这个错误“合并列表错误”,就像下面这样: E:Encountered a section with no Package: header, E:P ...
- IDEA被删除的模块在编译时会再次出现
工程根目录下.idea文件->compiler.xml,删除多余的model,workspace.xml->删除带有无用的target标签
- 模块化开发(requireJS)
模块化 在前端使用模块化开发,可以将代码根据功能实施模块的划分,每个模块功能(职责)单一,在需要更改对应的功能的时候,只需要对指定的模块进行修改,其他模块不受任何影响. 为什么要进行前端模块化? 达到 ...
- Vue入坑——vue-cli(脚手架)目录结构认识
转载:https://my.oschina.net/u/3802541/blog/1809182 一.目录结构 |-- build // 项目构建 ...
- openstack各组件介绍
Nova:计算服务,通过虚拟化技术,实现虚拟机的创建,管理,删除,是openstack中最核心的服务. Neutron:网络服务,为虚拟机提供网络连接服务,就像物理机的交换机和路由器一样 Glance ...
- pip的使用方法简介
pip是Python包管理工具,它提供了对Python包的查找.下载.安装.卸载的功能 目前如果你在 python.org 下载最新版本的安装包,则是已经自带了该工具. 以下是pip常用命令 显示版本 ...