package test;
/*两个字:蛋疼! 参考网上的程序掉坑里啦,还好捋过来了*/
import java.util.*;
public class Util {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String data = null;
int k = 0;
//输入数据
data = in.nextLine();
k = Integer.parseInt(in.nextLine());
//转化数据,拆分成单个字符存放到数组number中
char[] tempNumber = data.toCharArray();
int[] number = new int[tempNumber.length];
for (int i = 0; i < tempNumber.length; i++) {
number[i] = tempNumber[i] - 48;
}
//
int result = 0;
//申请number.length -1 个空隙,作为分割空隙
int[] space = new int[number.length -1];
for (int i = 0; i < Math.pow(2, number.length); i++) {
int tempData = i;
int flagIndex = 0;
for (int j = 0; j < number.length - 1; j++) {
space[j] = tempData%2;
tempData /= 2;
if (space[j] == 1) {
//假设space[j] = 1时作为分割符
flagIndex++;
}
}
int tempSum = 1;
if (flagIndex == k-1) {
int tempValue = number[number.length-1];
int count = 1;
System.out.print("(");
/*分割符的下表区间[0, number.length-2], 如果遇到1说明分割,此时tempValue=number[x]*/
for(int x = number.length -2; x >= 0; x--) {
if (space[x] == 0) {
tempValue += Math.pow(10, count)*number[x];
count++;
} else {
System.out.print(tempValue + "*");
tempSum*=tempValue;
tempValue = number[x];
count=1;
}
}
System.out.println( tempValue+ ")");
tempSum*=tempValue;
}
if (tempSum > result) result = tempSum;
}
System.out.println(result);
}
}
最大k乘积问题
最新推荐文章于 2020-12-21 17:02:16 发布