• [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 }
  • 相关阅读:
    外观模式
    虚拟专用网
    DHCP服务
    NFS文件服务器
    samba服务器
    fatal error: Invalid layout of preloaded class
    ftp上传与下载
    byte与char的区别
    android管理联系人操作
    android图像与图像处理系列(一、Bitmap和BitmapFactory)
  • 原文地址:https://www.cnblogs.com/PureJava/p/10498094.html
Copyright © 2020-2023  润新知