百度筆試——牛牛的卡片
題目
牛牛有n張卡片,每張卡片要么是0,要么是5,牛牛能從其中選出若干張卡片,然后組成一些數字,現在請找出所有的可能的數字里面能整除90的最大的數字,不存在則輸出-1。
解析
問題實質:如果一個數字里面的數的出現次數的累加和是9的倍數,那么他就可以被9整除。這題是90,只要我們在這個數末尾加0就可以了。
code:直接復制的大佬的代碼
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 Scanner sc = new Scanner(System.in); 4 int n = sc.nextInt(); 5 int count_0 = 0; 6 int count_5 = 0; 7 for (int i = 0; i < n; i++) { 8 int tmp = sc.nextInt(); 9 if (tmp == 0) 10 count_0++; 11 else 12 count_5++; 13 } 14 if (count_5 < 9 || count_0 < 1) {// 如果個數不滿9個,或者沒有0,則肯定不行 15 System.out.println(-1); 16 return; 17 } 18 while (count_5 % 9 != 0)//將5的個數減少到是9的整數倍 19 count_5--; 20 21 for (int i = 0; i < count_5 / 9; i++) { 22 System.out.print("555555555"); 23 } 24 for (int i = 0; i < count_0; i++) { 25 System.out.print("0"); 26 } 27 28 }
參考博客:https://blog.csdn.net/qq_35590091/article/details/108404974

浙公網安備 33010602011771號