如何写“所有这些数字是不同的” Java中的条件? [英] How to write an "all these numbers are different" condition in Java?
问题描述
好的,我有这个问题需要解决,但我无法正确编写Java。看下面的图片,你会看到一个6角星,每个点和线的交点都是一个字母。
OK, I have this problem to solve but I can’t program it in Java correctly. See the picture below, you’ll see a 6 pointed star were every point and intersection of lines is a letter.
分配是将数字1到12定位,使得四个球的所有线的总和为26,并且星的所有6个点的总和也是26。
这归结为:
The assignment is to position the numbers 1 to 12 in such a way that the sum of all lines of four balls is 26 and the sum of all the 6 points of the star is 26 as well. This comes down to:
- (A + C + F + H == 26)
- (A + D + G + K == 26)
- (B + C + D + E == 26)
- (B + F + I + L == 26)
- (E + G + J + L == 26)
- (H + I + J + K == 26)
- (A + B + E + H + K + L == 26)
- (A+C+F+H==26)
- (A+D+G+K==26)
- (B+C+D+E==26)
- (B+F+I+L==26)
- (E+G+J+L==26)
- (H+I+J+K==26)
- (A+B+E+H+K+L==26)
所以我开始编写一个程序,它将遍历所有选项,强制解决方案。循环正在运行,但是,它现在显示一个数字被多次使用的解决方案,这是不允许的。如何在代码中进行检查是否所有变量都不同?
So I started programming a program that would loop through all options brute forcing a solution. The loop is working, however, it now shows solutions where one number is used more than once, which is not allowed. How can I make it in the code that it also checks whether all variables are different or not?
if ((A!= B != C != D != E != F != G != H != I != J != K != L)
我尝试了上述内容,但它不起作用,因为它说:
I tried the above, but it doesn't work, because it says:
无与伦比types:boolean和int。
incomparable types: boolean and int.
如何在1或小语句中检查是否所有数字是不同的?
(而不是制作一个检查每个变量组合的嵌套12 * 12语句)
(instead of making a nested 12*12 statement which checks every variable combination)
这是我到目前为止的代码:
This is my code so far:
public class code {
public static void main(String[] args){
for(int A = 1; A < 13; A++){
for(int B = 1; B < 13; B++){
for(int C = 1; C < 13; C++){
for(int D = 1; D < 13; D++){
for(int E = 1; E < 13; E++){
for(int F = 1; F < 13; F++){
for(int G = 1; G < 13; G++){
for(int H = 1; H < 13; H++){
for(int I = 1; I < 13; I++){
for(int J = 1; J < 13; J++){
for(int K = 1; K < 13; K++){
for(int L = 1; L < 13; L++){
if ((A+C+F+H==26) && (A+D+G+K==26) && (B+C+D+E==26) && (B+F+I+L==26) && (E+G+J+L==26) && (H+I+J+K==26) && (A+B+E+H+K+L==26)){
if ((A= C != D != E != F != G != H != I != J != K != L)){
System.out.println("A: " + A);
System.out.println("B: " + B);
System.out.println("C: " + C);
System.out.println("D: " + D);
System.out.println("E: " + E);
System.out.println("F: " + F);
System.out.println("G: " + G);
System.out.println("H: " + H);
System.out.println("I: " + I);
System.out.println("J: " + J);
System.out.println("K: " + K);
System.out.println("L: " + L);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
推荐答案
您的嵌套循环将执行 12 ^ 12 = 8.91610045E12
IF-Statements ,其中许多因数字组合错误而无效。您需要排列 1,2,3,..,12
作为您的强制方法的候选人。 12个元素的排列数是 12!= 479 001 600
,因此我认为强制执行速度会快得多。只生成有效排列,您不需要检查有效组合。
Your nested loops will execute 12^12 = 8.91610045E12
IF-Statements, many of them invalid because of wrong combinations of numbers. You need permutations of 1,2,3,..,12
as candidates of your bruteforcing approach. The number of permutations of 12 Elements is 12!= 479 001 600
, so the bruteforcing will be much faster I guess. With only generating valid permutations you don't need any check for valid combinations.
这是一些示例代码,nextPerm()中的代码是从置换生成器:
Here is some sample code, the code in nextPerm() is copied and modified from Permutation Generator :
import java.util.Arrays;
public class Graph26 {
private static final int A = 0;
private static final int B = 1;
private static final int C = 2;
private static final int D = 3;
private static final int E = 4;
private static final int F = 5;
private static final int G = 6;
private static final int H = 7;
private static final int I = 8;
private static final int J = 9;
private static final int K = 10;
private static final int L = 11;
private final static boolean rule1(final int[] n) {
return n[A] + n[C] + n[F] + n[H] == 26;
}
private final static boolean rule2(final int[] n) {
return n[A] + n[D] + n[G] + n[K] == 26;
}
private final static boolean rule3(final int[] n) {
return n[H] + n[I] + n[J] + n[K] == 26;
}
private final static boolean rule4(final int[] n) {
return n[B] + n[C] + n[D] + n[E] == 26;
}
private final static boolean rule5(final int[] n) {
return n[B] + n[F] + n[I] + n[L] == 26;
}
private final static boolean rule6(final int[] n) {
return n[E] + n[G] + n[J] + n[L] == 26;
}
private final static boolean rule7(final int[] n) {
return n[A] + n[B] + n[E] + n[H] + n[K] + n[L] == 26;
}
private final static boolean isValid(final int[] nodes) {
return rule1(nodes) && rule2(nodes) && rule3(nodes) && rule4(nodes)
&& rule5(nodes) && rule6(nodes) && rule7(nodes);
}
class Permutation {
private final int[] o;
private boolean perms = true;
public boolean hasPerms() {
return perms;
}
Permutation(final int[] obj) {
o = obj.clone();
}
private int[] nextPerm() {
int temp;
int j = o.length - 2;
while (o[j] > o[j + 1]) {
j--;
if (j < 0) {
perms = false;
break;
}
}
if (perms) {
int k = o.length - 1;
while (o[j] > o[k]) {
k--;
}
temp = o[k];
o[k] = o[j];
o[j] = temp;
int r = o.length - 1;
int s = j + 1;
while (r > s) {
temp = o[s];
o[s] = o[r];
o[r] = temp;
r--;
s++;
}
}
return o.clone();
}
}
public static void main(final String[] args) {
int[] nodes = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
final Graph26 graph = new Graph26();
final Permutation p = graph.new Permutation(nodes);
int i = 0;
while (p.hasPerms()) {
if (isValid(nodes)) {
System.out.println(Arrays.toString(nodes));
}
i++;
nodes = p.nextPerm();
}
System.out.println(i);
}
}
这篇关于如何写“所有这些数字是不同的” Java中的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!