华为OD机试(2021-05)题目一

题目

输入一串数字,找出他们中的众数,再在众数中找中位数

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int[] a = {0,0,0,0,0,0,0,0,0,0};
        int unum = 0;
        int u = 0;

        for (int i = 0; i < String.valueOf(x).length(); i++){
            int n = (int) (x / (Math.pow(10, i))) % 10;
            a[n] = a[n]+1;
            if (a[n] > u){
                unum = a[n];
                u = n;
            }
        }

        int c = 0;
        int cnum = 0;
        for (int i = 0; i < String.valueOf(x).length(); i++){
            int n = (int) (x / (Math.pow(10, i))) % 10;
            if (n == u){
                cnum++;
                if (cnum == (unum/2)+1){
                    break;
                }
            }
            c++;
        }
        System.out.print(u+" "+c);
    }

你可能感兴趣的:(华为)