zoukankan      html  css  js  c++  java
  • 给你一个整型数组如{1,3,4,7,2,1,1,5,2}, * 打印出现次数最多的那个数,如果最多的次数相同,则打印数字大的那个数。

    /*
    * 给你一个整型数组如{1,3,4,7,2,1,1,5,2},
    * 打印出现次数最多的那个数,如果最多的次数相同,则打印数字大的那个数。
    */
    public class Test6 {

    public static void main(String[] args) {
        Test();
    }
    
    public static void Test() {
        int count1, count2 = 0, max = 0;
        int[] a = { 9, 3, 7, 7, 9, 9, 7, 10, 7, 5, 9, 7, 5, 8 };
        for (int i = 0; i < a.length; i++) {
            count1 = 0;
            for (int j = i; j < a.length; j++) {
    
                if (a[i] == a[j]) {
                    count1++;
                }
            }
    
            if (count2 < count1) {
                count2 = count1;
                max = a[i];
    
            } 
    
            else if (count1 == count2) {
                if (max < a[i]) {
                    max = a[i];
    
                }
            }
        }
        System.out.println("出现次数最多的数字是:" + max + ",出现的次数是:" + count2);
    }
    

    }

  • 相关阅读:
    java基础_面试题笔记
    ACM-ICPC 2018 Xuzhou Online Contest题解
    覆盖点问题总结
    2018icpc沈阳网络赛题解(转发)
    树链剖分
    树状数组
    线段树板子
    sdoi2016生成魔咒
    洛谷3804
    大佬博文收集
  • 原文地址:https://www.cnblogs.com/MountDa/p/13174809.html
Copyright © 2011-2022 走看看