阿里20届Android 模拟笔试题分享—火柴“拼”最大数字
题目: 输入火柴的数量和需要拼出的数字位数,求利用这堆火柴所能拼出该位数下的最大数字
其中拼出数字1需要2根火柴,数字2、3、5需要5根火柴,数字4需要4根火柴,数字6、9需要6根火柴,数字7需要3根火柴,数字8需要7根火柴
例如:
输入
20
7
输出
9771111
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();//火柴根数
int n = in.nextInt();//位数
System.out.println(maxNum(m, n));
}
static String maxNum(int m, int n) {
String result = "";
if (n == 1) {
if (m >= 6) {
return "9";
} else if (m >= 3) {
return "7";
} else {
return "1";
}
}
if (m >= (n - 1) * 2 + 6) {
return "9" + maxNum(m - 6, n - 1);
} else if (m >= (n - 1) * 2 + 3) {
return "7" + maxNum(m - 3, n - 1);
} else {
return "1" + maxNum(m - 2, n - 1);
}
}
}
SHEIN希音公司福利 283人发布
查看12道真题和解析

