编程语言
首页 > 编程语言> > PAT甲级 The Dominant Color (技巧+java版map)

PAT甲级 The Dominant Color (技巧+java版map)

作者:互联网

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 }

 

 
 

标签:24,map,Dominant,int,cin,util,color,java
来源: https://www.cnblogs.com/1013star/p/10356007.html