I have a question about Java code.

Asked 1 years ago, Updated 1 years ago, 376 views

import java.util.Arrays;

public class hellojava {
  public static void main(String[] args) {
    int[] arr1 = new int[7];

    int sum=0,sum1=0, sum2 = 0;
    int a, b, c, d, e, f, g;  
    int i,j;
    int count = 0;  

    for (a = 1; a < 8; a++) {
      arr1[0] = a;
      for (b = 1; b < 8; b++) {

        arr1[1] = b;
        for (c = 1; c < 8; c++) {
          arr1[2] = c;
          for (d = 1; d < 8; d++) {
            arr1[3] = d;
            for (e = 1; e < 8; e++) {
              arr1[4] = e;
              for (f = 1; f < 8; f++) {
                arr1[5] = f;
                for (g = 1; g < 8; g++) {

                  arr1[6] = g;
                  sum = arr1[2] + arr1[1] + arr1[0] + arr1[3];
                  sum1 = arr1[1] + arr1[0] + arr1[4] + arr1[5];
                  sum2 = arr1[0] + arr1[3] + arr1[5] + arr1[6];

                  if (sum == sum1 && sum == sum2 && sum2 == sum1)
                    count += 1;
                }
              }
            }
          }
        }
      }
    }
    System.out.println(count);
  }
}

I want to put numbers from 1 to 7 in the array and pick four each to find the number of cases where the sum is correct in three places, but I don't know how to catch the overlap. This is Java 1.8 version.

java

2023-01-09 22:32

1 Answers

There are a few ways, and you probably don't want to squeeze something like if (g == a || g == b || g == c || g == d || g == e || g == f).

The way to modify and implement the code to a minimum is to create an array such as boolean[] used = new boolean[8] and check which number was used in the for-loop.

boolean[] used = new boolean[8];

for (a = 1; a < 8; a++) {
  arr1[0] = a, used[a] = true; // a check that a is used
  for (b = 1; b < 8; b++) {
    If (used[b]) continue; // skip if b has already been used
    arr1[1] = b, used[b] = true; // check that b is used
    for (c = 1; c < 8; c++) {
      If (used[c]) continue; // skip if c has already been used
      arr1[2] = c, used[c] = true; // check that c is used
      for (d = 1; d < 8; d++) {
        // ...
      }
      used[c] = false; // c's use state should not accumulate, so c's use state should be released
    }
    used[b] = false; // disable b
  }
  used[a] = false; // disable a
}

Furthermore, you might think that the seven-fold for-loop is not something. Or you might need to extend this problem to a larger number of people. The methodology used in this case is backtracking. Practice Question 1 introduced in the link is similar to the question you presented, so it would be good to check it out.

Reimplementing this issue with backtracking will result in:

public class hellojava {
    // the number of cases
    public static int count = 0;

    // Is the i-th number in the arr? Then, true or false. Or it's
    public static boolean[] used = new boolean[8];

    public static int[] arr = new int[7];

    // index: which index of arr to populate
    public static void backtrack(int index) {
        // If index == 7, i.e. if arr is full, check condition
        if (index == 7) {
            int sum = arr[2] + arr[1] + arr[0] + arr[3];
            int sum1 = arr[1] + arr[0] + arr[4] + arr[5];
            int sum2 = arr[0] + arr[3] + arr[5] + arr[6];

            if (sum == sum1 && sum == sum2) {
                // If (sum == sum1 && sum == sum2), then (sum2 == sum1) conditions are not required.
                count += 1;
            }

            return;
        }

        // If not, you have to populate Arr's current index with numbers.
        for (int x = 0; x < 8; x++) {
            // If the arr already contains x, ignore it.
            if (used[x]) continue;
            // Indicates that there is an x in the arr.
            used[x] = true;
            // index. Let's look at the next index.
            backtrack(index + 1);
            // To see another case, undo the indication that x is in the arr.
            used[x] = false;
        }
    }

    public static void main(String[] args) {
        backtrack(0);
        System.out.println(count);
    }
}


2023-01-09 23:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.