Pat(Advanced Level)Practice--1054(The Dominant Color)
Pat1054代码
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.
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:
24
AC代码:(一)
#include<cstdio>
#include<map>using namespace std;int main(int argc,char *argv[])
{int i,j;int m,n;map<int,int> v;scanf("%d%d",&m,&n);for(i=0;i<n;i++)for(j=0;j<m;j++){int temp;scanf("%d",&temp);v[temp]++;}map<int,int>::iterator it;for(it=v.begin();it!=v.end();it++)if(it->second>m*n/2)break;printf("%d\n",it->first);return 0;
}
其实我觉得可以把本题转换成求众数的问题,不知道为什么最后一个case错误。还是贴出来以后再看吧!为什么???
#include<cstdio>
#include<vector>
#include<algorithm>using namespace std;int main(int argc,char *argv[])
{int i,j,len;int ans,temp;vector<int> v;int n,m;scanf("%d%d",&m,&n);for(i=0;i<n;i++){for(j=0;j<m;j++){scanf("%d",&temp);v.push_back(temp);}}sort(v.begin(),v.end());len=1;for(i=1;i<m*n;i++)if(v[i]==v[i-len]){len++;ans=v[i];if(len>n*m/2)break;}printf("%d\n",ans);return 0;
}
#include<cstdio>using namespace std;int main(int argc,char *argv[])
{int m,n;int i,j;int ans,count=0;scanf("%d%d",&m,&n);for(i=0;i<n;i++){for(j=0;j<m;j++){int temp;scanf("%d",&temp);if(count==0){ans=temp;count=1;}else{if(ans==temp)count++;elsecount--;}}}printf("%d\n",ans);return 0;
}
因为该数出现的次数超过一半,所以遇到不相同的数则计数减一,最后剩下的数肯定是出现次数最多的数。。。
#include<cstdio>
#include<vector>
#include<algorithm>using namespace std;int main(int argc,char *argv[])
{int i,j,len;int temp;vector<int> v;int n,m;scanf("%d%d",&m,&n);for(i=0;i<n;i++){for(j=0;j<m;j++){scanf("%d",&temp);v.push_back(temp);}}sort(v.begin(),v.end());printf("%d\n",v[(m*n)/2]);return 0;
}
Pat(Advanced Level)Practice--1054(The Dominant Color)相关推荐
- PAT (Advanced Level) Practice 题解代码 - II (1051-1100)
PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...
- PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- 【PAT (Advanced Level) Practice】1054 The Dominant Color (20 分)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...
- PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)
1001 A+B Format (20 分) 题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种. #include<bits/stdc++.h> using ...
- 【PAT (Advanced Level) Practice】1093 Count PAT‘s (25 分)
1093 Count PAT's (25 分) The string APPAPT contains two PAT's as substrings. The first one is formed ...
- 【PAT (Advanced Level) Practice】1124 Raffle for Weibo Followers (20 分)
1124 Raffle for Weibo Followers (20 分) John got a full mark on PAT. He was so happy that he decided ...
- 【PAT (Advanced Level) Practice】1086 Tree Traversals Again (25 分)
众所周知只有前序遍历和后序遍历是不可能推出中序遍历的,所以我就不废话了 由树的前序遍历和中序遍历可以推后序遍历 由树的中序遍历和后序遍历可以推前序遍历 #include <cstdio> ...
- 【PAT (Advanced Level) Practice】1051 Pop Sequence (25 分)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- 【PAT (Advanced Level) Practice】1037 Magic Coupon (25 分)
题意: 给出两个集合,从这两个集合里面选出数量相同的元素进行一对一相乘,求能够得到的最大乘积之和. 题解: 对每个集合,将正数和负数分开考虑,将每个集合里的整数从大到小排序:将每个集合里的负数从小到大 ...
最新文章
- 深入Java集合学习系列:ArrayList的实现原理
- c语言线程面试题,java多线程面试题 PDF 下载
- java获取tomcat路径
- spring教程(上)
- 计算机网闸合同,网闸三大主流技术 -电脑资料
- OOP_面向对象程序设计概述
- VC 2015 x86的DLL绿色包(QT 5.6)
- 【JAVA】每日练习——02
- 使用HTML5 Canvas API中的clip()方法裁剪区域图像
- python数学符号读法大全_数学符号读法大全
- 做XH2.54杜邦线材料-导线
- 某CV四小龙继续裁员,AI独角兽还可以去吗?
- LTE中资源数量映射用到的PRB数量(TB,CQI,MCS,PRB)
- 使用 Microsoft Teams Toolkit for Visual Studio 高效构建一个指示板
- 京东2020校招数据分析工程师二面(2019.9.18)
- 查看表空间和解决表空间扩容ORA-01119:ORA-27040问题
- 基于观测器的T-S模糊系统故障分析simulink仿真
- SpringBoot + Spring data JPA使用方言(自定义函数、一些自带函数)
- 【开源】从零构建NB-IOT物联网项目开发,代码实现功能:人体红外传感
- 视图层、WXML语法、WXSS样式、事件、WXS脚本语法