zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    python——时间与时间戳之间的转换
    Python3中正则模块re.compile、re.match及re.search
    javascript 模块化开发
    Python细说 xrange 和 range 的区别
    PyInstaller 生成exe文件
    win10安装mysql5.7.14winx64遇到服务无法启动问题解决方法
    Python 自定义队列 数据结构
    spring事务使用心得
    LS 存取文件
    Single Instance Application
  • 原文地址:https://www.cnblogs.com/PureJava/p/10498094.html
Copyright © 2011-2022 走看看