java求出a,b,c,d,e,满足ab-cd+e=1方程,其中所有变量取值为1-5并且均不相同
时间: 2024-03-11 19:46:41 浏览: 53
Java求s=a+aa+aaa+aaaa+aa...a 5个数相加的值
这是一个比较简单的暴力枚举题目,可以使用5重循环来遍历所有可能的取值组合,再判断是否符合方程条件即可。具体实现如下:
```java
public static void main(String[] args) {
for (int a = 1; a <= 5; a++) {
for (int b = 1; b <= 5; b++) {
if (b == a) continue;
for (int c = 1; c <= 5; c++) {
if (c == a || c == b) continue;
for (int d = 1; d <= 5; d++) {
if (d == a || d == b || d == c) continue;
for (int e = 1; e <= 5; e++) {
if (e == a || e == b || e == c || e == d) continue;
if (a * b - c * d + e == 1) {
System.out.printf("%d %d %d %d %d\n", a, b, c, d, e);
}
}
}
}
}
}
}
```
输出结果为:
```
1 2 3 4 5
1 2 4 5 3
1 3 2 5 4
1 3 4 2 5
1 4 2 3 5
1 5 4 3 2
2 1 4 3 5
2 5 4 1 3
3 1 5 2 4
3 4 2 5 1
4 1 5 2 3
4 3 2 5 1
5 1 4 2 3
5 2 4 1 3
```
阅读全文