How to write in Java "all these numbers are different"?

OK, I have this problem, but I cannot program it correctly in Java. See the figure below, you will see a six-pointed star, each point and the intersection of lines is a letter.

The purpose is to arrange numbers from 1 to 12 so that the sum of all rows of four balls is 26, and the sum of all 6 points of the star is 26. This reduces 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)

So, I began to program a program that will cycle through all the parameters that force the solution. The cycle works, but now it shows solutions in which one number is used more than once, which is unacceptable. How can I do this in the code so that it also checks if the variables are different or not?

if ((A!= B != C != D != E != F != G != H != I != J != K != L) 

I tried above, but this does not work because it says:

incomparable types: boolean and int.

How can I do a check inside 1 or a small operator to see if all numbers are different?

(instead of creating a nested 12 * 12 instruction that checks each combination of variables)

This is my code:

  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); } } } } } } } } } } } } } } } } 
+8
java if-statement
source share
4 answers

Nested loops will execute 12^12 = 8.91610045E12 IF-Statement, many of which are invalid due to incorrect number combinations. You need permutations 1,2,3,..,12 as candidates for your approach using brute force. The number of permutations of 12 elements is 12!= 479 001 600 , so brute force will be much faster, I think. If you create only valid permutations, you do not need to check the correct combinations.

Here is a sample code, the code in nextPerm () is copied and modified from a 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); } } 
+1
source share

If I get it right, you want to check if all ALs are unique. So just put them in a set and find the size of the set:

 if ((new HashSet<Integer>( Arrays.asList(A, B, C, D, E, F, G, H, I, J, K, L))) .size() == 12) { //do your stuff } 
+11
source share

I highly recommend using recursion instead, which will greatly simplify the code. Do something like this:

 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) 

However, to answer your question directly: you can throw all the values ​​into set and check that the size is equal to the number of elements that you have nested. This works because the collection will consider duplicates as one.

+5
source share

Before looking for a good solution for you, I would like to help with the error you get.

if ((A= C != D != E != F != G != H != I != J != K != L)){

This line does not make much sense. The first thing the compiler will check is:

if (A=C)

You probably would like to code if (A!=C) , but think about what you are really typing. A=C is attribution, so A will get the value C

Then the compiler will continue. After assigning C A , he will check the comparison:

if (A=C != D)

This compares the value of A with D , which will lead to a logical - let's say that the result is false .

The following comparison would be as follows:

if (false != E)

At this point, a comparison occurs between Boolean and int, hence the incomparable types: boolean and int. error incomparable types: boolean and int. .

Well, since you need to verify that your numbers are unique, a good solution would be what @ abhin4v suggested .

+2
source share

Source: https://habr.com/ru/post/649866/


All Articles