这个问题还是很简单的(我不是大佬,不知道还有没有更好的算法),问题的关键在于这类数是有规律可循的,话不多说直接上代码
[Java] 纯文本查看 复制代码 package com.lpc.pack;
import java.util.Scanner;
public class ResolveNumber {
public static void main(String[] args) {
System.out.println("请输入一个正整数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
int length = String.valueOf(num).length(); // 整数的位数
int[] place = new int[length]; // 数量级
for (int i = 0; i < length; i++) {
place[length - 1 - i] = (int) Math.pow(10, i);
}
int[] count = new int[length]; // 位对应的数
int temp = num;
for (int i = 0; i < length; i++) {
count[i] = temp / place[i];
temp -= count[i] * place[i];
}
System.out.println(num + "可分解为:");
int min;
int originalValue;
for (int i = 0; i < length; i++) {
originalValue = place[i];
while (count[i] != 0) {
min = findMin(count, i); // 位对应的数的最小值
count[i] = count[i] - min;
for (int j = i + 1; j < length; j++) {
if (count[j] == 0) {
continue;
}
count[j] = count[j] - min;
place[i] += place[j];
}
System.out.println(min + "个:" + place[i]);
place[i] = originalValue;
}
}
}
private static int findMin(int[] count, int m) {
int min = count[m];
for (int i = m + 1; i < count.length; i++) {
if (min > count[i] && count[i] != 0) {
min = count[i];
}
}
return min;
}
}
|