Baidu 2021秋の募集——0903最初の質問は、90で割った最大数を見つけることです

トピック:
Niuniuにはカードがn枚あります。各カードは0または5のいずれかです。Niuniuはいくつかの数字を選択していくつかの数字を形成できます。ここでNiuniuは、90で割り切れる最大の数字すべてを見つけるように依頼します。存在しない場合は、出力- 1

入力の説明:
最初の行には正の整数n
が含まれ次の行にはnの整数ai
1 <= 0 <= 10 ^ 3
ai = 0またはai = 5が含まれています

出力の説明
回答を示す数字を出力します


入力
11
5 5 5 5 5 5 5 5 5 5 0
出力
5555555550

アイデア:
0と5の数に応じて直接答えて、90を除算します。5の数は9の倍数であり、最後の桁は0でなければならず、次のコード実装です。

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};
        }
    }
}

注:このコードはAC 100%です。より良い方法がある場合は、コメントしてください

おすすめ

転載: blog.csdn.net/weixin_43419256/article/details/108393494