Baidu 2021 Autumn Recruitment——0903 The first question is to find the largest number that is divided by 90

Topic:
Niuniu has n cards. Each card is either 0 or 5. Niuniu can select some numbers to form some numbers. Now Niuniu would like to ask you to find all the largest numbers that can be divided evenly by 90. If it does not exist, output- 1

Input description: The
first line contains a positive integer n and the
next line contains n integers ai
1<=0<=10^3
ai = 0 or ai= 5

Output description
Output a number to indicate the answer

Example
Input:
11
5 5 5 5 5 5 5 5 5 5 0
Output
5555555550

Idea:
Answer directly according to the number of 0 and 5, divide 90, the number of 5 is a multiple of 9, and the last digit must be 0, the next code implementation:

package QiuZhao_0903;

import java.util.Scanner;

public class First {
    
    

    public static void main(String[] args) {
    
    
        int count0 = 0;
        int count5 = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
    
    
            arr[i] = sc.nextInt();
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            if (arr[i] == 5) {
    
    
                count5++;
            } else {
    
    
                count0++;
            }
        }

        for (int i = 0; i < judge(count0, count5).length; i++) {
    
    
            System.out.print(judge(count0, count5)[i]);
        }
    }

    public static int[] judge(int count0, int count5) {
    
    
        if (count0 == 0) return new int[]{
    
    -1};
        if (count5 >= 9 && count0 > 0) {
    
    
            int[] res = new int[(count5 / 9) * 9 + count0];
            for (int i = 0; i < res.length - count0; ) {
    
    
                res[i] = 5;
                i++;
                res[res.length - count0] = 0;
            }
            return res;
        } else {
    
    
            return new int[]{
    
    0};
        }
    }
}

Note: This code is AC 100%, if there is a better method, please comment

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/108393494