[PAT] 1054 The Dominant Color (20 分)Java

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, 800×600), 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

复制代码
 1 package pattest;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * @Auther: Xingzheng Wang
 7  * @Date: 2019/2/23 19:48
 8  * @Description: pattest
 9  * @Version: 1.0
10  */
11 public class PAT1054 {
12     public static void main(String[] args) {
13         Scanner sc = new Scanner(System.in);
14         int n = sc.nextInt();
15         int m = sc.nextInt();
16         Map<Integer, Integer> map = new HashMap<>();
17         for (int i = 0; i < n * m; i++) {
18             int temp = sc.nextInt();
19             if (map.containsKey(temp)) {
20                 int value = map.get(temp);
21                 value++;
22                 map.put(temp, value);
23             } else {
24                 int value = 1;
25                 map.put(temp, value);
26             }
27 
28         }
29         map = sortByValue(map);
30         for (Map.Entry entry : map.entrySet()) {
31             System.out.println(entry.getKey());
32             break;
33         }
34     }
35     //map按照value排序
36     public static <K,V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K,V> map){
37         List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
38         Collections.sort(list, (o1, o2) -> {
39             int compare = (o1.getValue()).compareTo(o2.getValue());
40             return -compare;
41         });
42 
43         Map<K, V> result = new LinkedHashMap<K, V>();
44         for (Map.Entry<K, V> entry : list) {
45             result.put(entry.getKey(), entry.getValue());
46         }
47         return result;
48     }
49 }
复制代码

 

posted @ 2019-03-08 20:35  Pure_Java  阅读(214)  评论(0)    收藏  举报
编辑推荐:
· 《C#高级GDI+实战:从零开发一个流程图》增加贝塞尔曲线
· AES 加密模式演进:从 ECB、CBC 到 GCM 的 C# 深度实践
· InnoDB为什么不用跳表,Redis为什么不用B+树?
· 记一次 C# 平台调用中因非托管 union 类型导致的内存访问越界
· [EF Core]聊聊“复合”属性
阅读排行:
· 博客园众包:再次诚征3D影像景深延拓实时处理方案(预算8-15万,需求有调整)
· 扣子(Coze),开源了!Dify 天塌了
· 精选 5 款 .NET 开源、功能强大的工作流系统,告别重复造轮子!
· 爆肝2月,我的 AI 代码生成平台上线了!
· 从经典产品看大模型方向
点击右上角即可分享
微信分享提示