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;
}
AC代码:(二)
#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;
}

因为该数出现的次数超过一半,所以遇到不相同的数则计数减一,最后剩下的数肯定是出现次数最多的数。。。

AC代码:(三)因为该数出现的次数超过一半,所以排序后中位数一定是它。。。
#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)相关推荐

  1. PAT (Advanced Level) Practice 题解代码 - II (1051-1100)

    PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)

    1001 A+B Format (20 分) 题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种. #include<bits/stdc++.h> using ...

  6. 【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 ...

  7. 【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 ...

  8. 【PAT (Advanced Level) Practice】1086 Tree Traversals Again (25 分)

    众所周知只有前序遍历和后序遍历是不可能推出中序遍历的,所以我就不废话了 由树的前序遍历和中序遍历可以推后序遍历 由树的中序遍历和后序遍历可以推前序遍历 #include <cstdio> ...

  9. 【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 ...

  10. 【PAT (Advanced Level) Practice】1037 Magic Coupon (25 分)

    题意: 给出两个集合,从这两个集合里面选出数量相同的元素进行一对一相乘,求能够得到的最大乘积之和. 题解: 对每个集合,将正数和负数分开考虑,将每个集合里的整数从大到小排序:将每个集合里的负数从小到大 ...

最新文章

  1. 深入Java集合学习系列:ArrayList的实现原理
  2. c语言线程面试题,java多线程面试题 PDF 下载
  3. java获取tomcat路径
  4. spring教程(上)
  5. 计算机网闸合同,网闸三大主流技术 -电脑资料
  6. OOP_面向对象程序设计概述
  7. VC 2015 x86的DLL绿色包(QT 5.6)
  8. 【JAVA】每日练习——02
  9. 使用HTML5 Canvas API中的clip()方法裁剪区域图像
  10. python数学符号读法大全_数学符号读法大全
  11. 做XH2.54杜邦线材料-导线
  12. 某CV四小龙继续裁员,AI独角兽还可以去吗?
  13. LTE中资源数量映射用到的PRB数量(TB,CQI,MCS,PRB)
  14. 使用 Microsoft Teams Toolkit for Visual Studio 高效构建一个指示板
  15. 京东2020校招数据分析工程师二面(2019.9.18)
  16. 查看表空间和解决表空间扩容ORA-01119:ORA-27040问题
  17. 基于观测器的T-S模糊系统故障分析simulink仿真
  18. SpringBoot + Spring data JPA使用方言(自定义函数、一些自带函数)
  19. 【开源】从零构建NB-IOT物联网项目开发,代码实现功能:人体红外传感
  20. 视图层、WXML语法、WXSS样式、事件、WXS脚本语法

热门文章

  1. 聚合数据api的使用
  2. Mac air装 win10 ,总是提示拷贝windows安装文件失败!?
  3. 幼儿园案例经验迁移_在建构区中如何将游戏经验迁移为知识经验
  4. 使用mac制作linux启动盘与恢复U盘(dd命令制作U盘启动盘后怎么恢复U盘)
  5. 50台同样配置的计算机装系统,几十台PC如何同时安装系统
  6. elasticsearch 过期数据自动删除Java代码
  7. 如何在Tanzu Cluster中使用vSphere with Tanzu内置容器注册表
  8. CMOS反相器设计与仿真
  9. 十月,再见;你好,十一月
  10. 神奇的递归!一文读懂函数递归(python实现)