java
输入一个数集。找出所有形如abc*de(三位数乘以两位数)的算式。使得在完整的竖式中,所有数字都属于一个特定的数字集合(全由数集中的数字组成)
输出数字集合(相邻数字之间无空格)。
输入:2357
输出:
<1>
775
X 33
------
2325
2325
------
25575
import java.util.Scanner;
public class Ma {
public static void main(String[] args) {
int tot;
int n;
int m;
String result;
String str;
int count = 0;
boolean ok = true;
Scanner scanner = new Scanner(System.in);
str = scanner.nextLine();
for (int abc = 100; abc <= 999; abc++) {
for (int de = 10; de <= 99; de++) {
// 得数
tot = abc * de;
// 乘个位
n = abc * (de % 10);
// 乘十位
m = abc * (de / 10);
// 将出现的数字放到一个新字符串里
result = "" + abc + de + tot + n + m;
ok = true;
// 检索出现的数字是否都在输入的集合里
for (int k = 0; k < result.length(); k++) {
// 不在返回FALSE
if (!str.contains(result.charAt(k) + "")) {
ok = false;
break;
}
}
// 如果在,输出这条竖式
if (ok) {
// 第 x 条
System.out.println("\n<" + (++count) + ">");
// 格式化输出
System.out.printf("%5d\nX%4d\n----\n%5d\n%4d\n----\n%5d\n", abc, de, n, m, tot);
}
}
}
System.out.println("The number of solutions= " + count);
}
}