如何在Java中编写"所有这些数字都不同"的条件?

Jav*_*aaa 8 java if-statement

好的,我有这个问题要解决,但我不能正确地用Java编程.看下面的图片,你会看到一个6角星,每个点和线的交点都是一个字母.

分配是以这样的方式定位数字1到12,使得四个球的所有线的总和是26并且星的所有6个点的总和也是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)

所以我开始编写一个程序,可以遍历所有选项,强制解决方案.循环正在运行,但是,它现在显示一个数字被多次使用的解决方案,这是不允许的.如何在代码中创建它还检查所有变量是否不同?

if ((A!= B != C != D != E != F != G != H != I != J != K != L)
Run Code Online (Sandbox Code Playgroud)

我尝试了上述方法,但它不起作用,因为它说:

无与伦比的类型:boolean和int.

如何在1或小型声明中检查所有数字是否不同?

(而不是制作一个嵌套的12*12语句来检查每个变量组合)

到目前为止这是我的代码:

    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);
                 }
                }
               }
              }
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }

}
Run Code Online (Sandbox Code Playgroud)

Abh*_*kar 11

如果我弄错了,你想检查所有A到L是否都是唯一的.所以只需将它们放在一个集合中,找到集合的大小:

if ((new HashSet<Integer>(
        Arrays.asList(A, B, C, D, E, F, G, H, I, J, K, L)))
    .size() == 12) {
    //do your stuff
}
Run Code Online (Sandbox Code Playgroud)


mar*_*cog 5

我强烈建议使用递归,这将极大地简化代码.做这样的事情:

function generate(set used, array list):
  if list.size() == 12:
    if list matches criteria:
      yield list as solution
  else:
    for next = 1; next < 13; next++:
      if next not in used:
        used.add(next)
        generate(used, list + next)
        used.remove(next)
Run Code Online (Sandbox Code Playgroud)

但是,直接回答你的问题:你可以将所有值都抛出到一个集合中,并检查它的大小是否等于你投入的项目数量.这是有效的,因为一个集合将重复计为一个.


Mic*_*zka 1

您的嵌套循环将执行12^12 = 8.91610045E12IF 语句,其中许多语句由于数字组合错误而无效。您需要对暴力破解方法的候选者进行排列。1,2,3,..,1212 个元素的排列数为12!= 479 001 600,所以我猜暴力破解会快得多。仅生成有效排列,您不需要对有效组合进行任何检查。

这是一些示例代码,nextPerm() 中的代码是从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);
    }
}
Run Code Online (Sandbox Code Playgroud)