PAT甲级 The Dominant Color (技巧+java版map)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PAT甲级 The Dominant Color (技巧+java版map),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3312字,纯文字阅读大概需要5分钟。
内容图文
![PAT甲级 The Dominant Color (技巧+java版map)](/upload/InfoBanner/zyjiaocheng/842/7f222a9f7e024db4a6ab00f70079eb17.jpg)
The Dominant Color
链接:https://www.nowcoder.com/questionTerminal/0495013675774f008541ea371eb5af17
来源:牛客网
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.示例1
输入
5 3 0 0 255 16777215 24 24 24 0 0 24 24 0 24 24 24
输出
24
题意:
输出给出的数中的众数
题解:技巧版
因为答案数的个数一定会超过总数的一半 所以可以当当前数是答案数的时候,计数器加一,不是的时候,计数器减一,最后不能完全抵消的就是答案。
1 import java.util.Scanner; 2 public class Main { 3 static int n,m; 4 public static void main(String[] args) { 5 Scanner cin = new Scanner(System.in); 6 m = cin.nextInt(); 7 n = cin.nextInt(); 8 int ans = 0,cnt=0; 9 for(int i=0;i<m;i++) { 10 for(int j=0;j<n;j++) { 11 int x = cin.nextInt(); 12 if(cnt == 0) { 13 ans = x; 14 cnt = 1; 15 } 16 else { 17 if(x == ans) { 18 cnt++; 19 } 20 else { 21 cnt--; 22 } 23 } 24 } 25 } 26 System.out.println(ans); 27 } 28 }
然后是java的map版
1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4 import java.util.Map.Entry; 5 import java.util.Scanner; 6 7 8 public class Main{ 9 static int n,m; 10 public static void main(String[] args) { 11 Scanner cin = new Scanner(System.in); 12 m = cin.nextInt(); 13 n = cin.nextInt(); 14 int ans = 0,cnt=0; 15 int value = 0;int x; 16 Map<Integer, Integer> map = new HashMap<Integer,Integer>(); 17 for(int i=0;i<m;i++) { 18 for(int j=0;j<n;j++) { 19 x = cin.nextInt(); 20 if(map.get(x) == null) { 21 value = 1; 22 } 23 else { 24 value = (int)map.get(x); 25 value++; 26 } 27 map.put(x, value); 28 } 29 } 30 Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator(); 31 int max = 0,res=0; 32 while(it.hasNext()) { 33 Entry<Integer, Integer> entry = it.next(); 34 if(entry.getValue()>max) { 35 res = entry.getKey(); 36 max = entry.getValue(); 37 } 38 } 39 System.out.println(res); 40 } 41 }
内容总结
以上是互联网集市为您收集整理的PAT甲级 The Dominant Color (技巧+java版map)全部内容,希望文章能够帮你解决PAT甲级 The Dominant Color (技巧+java版map)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。