百度2021秋招——0903 第一题 求整除90的最大数

题目:
牛牛有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%,如有更优方法,欢迎评论

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值