百度笔试——牛牛的卡片

题目

  牛牛有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     }
View Code

参考博客:https://blog.csdn.net/qq_35590091/article/details/108404974

  

原文地址:https://www.cnblogs.com/dream-flying/p/13622454.html