题目:
牛牛有n张卡片,每张卡片要么是0,要么是5,牛牛能从中选出一些组成一些数字,现在牛牛想请你找出所有可能整除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%,如有更优方法,欢迎评论