题目描述

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. html编写的日历

    1.html (1) <html> <head> <meta http-equiv="Content-Type" content="text ...

  2. 三角形的实现和盒模型、层模型、浮动模型、定位、权重、margin问题

    相邻的border会平分所占的区域,出现一个斜线, .my_triangle{ width: 10px; height: 10px; background-color: blue; border-wi ...

  3. flex:1将页面铺满

    代码示范: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. laravel sql mode only_full_group_by 解决小记

    環境: mysql: 5.7.* Laravel: 5.4.* sql 中使用到了 group by,會提示 500錯誤,將 config/database.php中的 strict的值改爲true, ...

  5. All you need to know about: solder mask and paste mask

    1, 从字面理解 (1) 从字面理解,solder mask意指要mask住需要solder的地方.那么被mask的是谁呢?是绿油层.可以把默认形态的绿油层想象成与PCB板形状.面积相同,solder ...

  6. 校园商铺-2项目设计和框架搭建-9验证Service

    1. 新建接口 main: com.csj2018.o2o.service/AreaService.java package com.csj2018.o2o.service; import java. ...

  7. HashMap 什么时候进行扩容呢

    HashMap扩容: 当HashMap中的元素越来越多的时候,碰撞的几率也就越来越高(因为数组的长度是固定的),所以为了提高查询的效率,就要对HashMap的数组进行扩容,数组扩容这个操作也会出现在A ...

  8. thinkphp 模板注释

    模板支持注释功能,该注释文字在最终页面不会显示,仅供模板制作人员参考和识别. 大理石平台厂家 单行注释 格式: {/* 注释内容 */ } 或 {// 注释内容 } 例如: {// 这是模板注释内容 ...

  9. 「题解」:$e$

    问题 B: $e$ 时间限制: 2 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 话说一天考两个主席树这回事…… 正解可以叫树上主席树??(脸哥说也叫主席树上树???) 对于树上的每 ...

  10. 线性dp——cf1096D

    dp[i][j]表示到第i位,与hard的匹配状态到达了第j位 每位有两种决策:消或者不消 分别转移一下即可 转移代码 ;i<n;++i) ;j<=;++j) { cmin(f[i+][j ...