JAVA语言用1, 2, 3...9 这九个数字组成一个数学公式,满足:ABC + DEF = GHI,每个数字只能出现一次,编写程序输出所有的组合。
时间: 2024-03-18 15:38:56 浏览: 108
Java的 String 类常用方法大全
下面是用Java语言编写的程序,可以输出所有符合要求的组合:
```java
public class Main {
public static void main(String[] args) {
for(int a = 1; a <= 9; a++) {
for(int b = 1; b <= 9; b++) {
if(b == a) continue;
for(int c = 1; c <= 9; c++) {
if(c == a || c == b) continue;
for(int d = 1; d <= 9; d++) {
if(d == a || d == b || d == c) continue;
for(int e = 1; e <= 9; e++) {
if(e == a || e == b || e == c || e == d) continue;
for(int f = 1; f <= 9; f++) {
if(f == a || f == b || f == c || f == d || f == e) continue;
for(int g = 1; g <= 9; g++) {
if(g == a || g == b || g == c || g == d || g == e || g == f) continue;
for(int h = 1; h <= 9; h++) {
if(h == a || h == b || h == c || h == d || h == e || h == f || h == g) continue;
int abc = a * 100 + b * 10 + c;
int def = d * 100 + e * 10 + f;
int ghi = g * 100 + h * 10 + i;
if(abc + def == ghi) {
System.out.println(abc + " + " + def + " = " + ghi);
}
}
}
}
}
}
}
}
}
}
}
```
这个程序使用了6层嵌套的循环来枚举所有可能的组合,并且通过判断每个数字是否已经被使用过来避免重复。最后,将符合要求的组合输出到控制台上。
阅读全文