百度2021java筆試

n個由0和5組成的數,找出全部可能的數字中最大能整除90的最大數字,不存在則輸出-1。

先看有沒有0,沒有0直接輸出-1,而後看剩下的5能分紅幾組9個5一組的。由於數字和爲9的數能被9整除java

import java.util.Scanner;

public class Solution6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int c0 = 0;
        int c5 = 0;
        for (int i = 0; i < num; i++) {
            int n = sc.nextInt();
            if(n == 0){
                c0 ++;
            }else{
                c5 ++;
            }
        }
        c5 = c5 / 9;
        if(c0 == 0 ){
            System.out.println(-1);
        }else if(c5 == 0){//0是能被任何非零天然數整除(90)
        	//*當組成的數小於5555555550時,最大能整除90的數字是0*
            System.out.println(0);
        }else {
            while (c5 > 0){
                System.out.println("555555555");
                c5--;
            }
            while (c0 > 0){
                System.out.println("0");
                c0--;
            }
        }
    }
}