查找所有具有重复元素但没有重复行的组合。

huangapple go评论58阅读模式
英文:

Find all combinations with repetitions but without duplicate rows

问题

以下是翻译好的部分:

有一个数组1、2、3、4。需要生成所有可能的3个元素的组合,元素可以重复使用。行中元素的顺序不重要。例如:114 = 411 = 141。我找不到合适的算法。我找到了这个算法,但不能有元素的重复,比如111或113,只能是123、124等。

public void doit(){
String[] arr = {"1", "2", "3","4"};
int count = fuctorial(arr.length);
int max = arr.length - 1;
System.out.println("Variations: " + count);
int shift = max;
String t;
while (count > 0) {
    t = arr[shift];
    arr[shift] = arr[shift - 1];
    arr[shift - 1] = t;
    print(arr);
    count--;
    if (shift < 2) {
        shift = max;
    } else {
        shift--;
    }
}
}
static void print(String[] arr) {
System.out.println(Arrays.toString(arr));
}

static int fuctorial(int n) {
return (n > 0) ? n * fuctorial(n - 1) : 1;
}
英文:

There is an array of 1, 2, 3, 4. It is necessary to make all possible combinations of 3 size series, the elements can be repeated. The order of the elements in the row is not important. For instance:
114 = 411 = 141.
I can't find a suitable algorithm. I have found this algorithm, but there can be no repetitions of elements, like 111 or 113, only 123,124 etc.

public void doit(){
String[] arr = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;,&quot;4&quot;};
            int count = fuctorial(arr.length);
            int max = arr.length - 1;
            System.out.println(&quot;Вариантов &quot; + count);
            int shift = max;
            String t;
            while (count &gt; 0) {
                t = arr[shift];
                arr[shift] = arr[shift - 1];
                arr[shift - 1] = t;
                print(arr);
                count--;
                if (shift &lt; 2) {
                    shift = max;
                } else {
                    shift--;
                }
            }
}
    static void print(String[] arr) {
        System.out.println(Arrays.toString(arr));
    }
 
    static int fuctorial(int n) {
        return (n &gt; 0) ? n * fuctorial(n - 1) : 1;
    }

答案1

得分: 2

请尝试这个代码:

static void combination(int[] a, int n) {
    int size = a.length;
    int[] selected = new int[n];
    new Object() {

        void print() {
            for (int i = 0; i < n; ++i)
                System.out.print(a[selected[i]] + " ");
            System.out.println();
        }

        void combination(int index, int prev) {
            if (index >= n)
                print();
            else
                for (int i = prev; i < size; ++i)
                    combination(index + 1, selected[index] = i);
        }

    }.combination(0, 0);
}

int[] a = {6, 7, 8, 9};
combination(a, 3);

输出:

6 6 6 
6 6 7 
6 6 8 
6 6 9 
6 7 7 
6 7 8 
6 7 9 
6 8 8 
6 8 9 
6 9 9 
7 7 7 
7 7 8 
7 7 9 
7 8 8 
7 8 9 
7 9 9 
8 8 8 
8 8 9 
8 9 9 
9 9 9 
英文:

Try this.

static void combination(int[] a, int n) {
    int size = a.length;
    int[] selected = new int[n];
    new Object() {

        void print() {
            for (int i = 0; i &lt; n; ++i)
                System.out.print(a[selected[i]] + &quot; &quot;);
            System.out.println();
        }

        void combination(int index, int prev) {
            if (index &gt;= n)
                print();
            else
                for (int i = prev; i &lt; size; ++i)
                    combination(index + 1, selected[index] = i);
        }

    }.combination(0, 0);
}

and

int[] a = {6, 7, 8, 9};
combination(a, 3);

output:

6 6 6 
6 6 7 
6 6 8 
6 6 9 
6 7 7 
6 7 8 
6 7 9 
6 8 8 
6 8 9 
6 9 9 
7 7 7 
7 7 8 
7 7 9 
7 8 8 
7 8 9 
7 9 9 
8 8 8 
8 8 9 
8 9 9 
9 9 9 

答案2

得分: 1

public void func() {
    boolean[] check = new boolean[49];
    for(int i=1; i <= 4; i++ ) {
        for(int j=1; j <= 4; j++) {
            for(int k=1; k <= 4; k++) {
                int sum = i*i + j*j + k*k;
                if(!check[sum]) {
                    check[sum] = true;
                    System.out.println(i + "," + j + "," + k);
                }
            }
        }
    }
}

思路是我们取一个三元组计算平方和然后检查是否已经有一个具有相同平方和的三元组相同的三元组将具有相同的平方和
平方和总是在3-48的范围内此外平方和对于每组数字组合都是唯一的正如您所要求的那样

复杂度为O(N^3)其中N是数组的大小因为我们需要3个元素的组合我认为无法降低复杂度

**更新** 为了更通用可以使用一个HashSet<Integer>来存储平方和而不是使用布尔数组并在输入数组上进行三重嵌套循环计算平方和并与HashSet进行比较

性能优化预先计算数组中每个元素的平方这样就不必一遍又一遍地计算它们
英文:
public void func() {
    boolean[] check = new boolean[49];
    for(int i=1; i &lt;=4; i++ ) {
        for(int j=1; j &lt;=4; j++) {
            for(int k=1; k&lt;=4; k++) {
                int sum = i*i + j*j + k*k;
                if(!check[sum]) {
                    check[sum] = true;
                    System.out.println(i + &quot;,&quot; + j + &quot;,&quot; + k);
                }
            }
        }
    }
}

idea is: we take a triplet, calculate the sum of squares, and check if we already had a triplet with that sum. identical triplets will have the same sum of squares.
the sum of squares will always be in the range 3-48. also, the sum is unique to each combination of numbers just like you require.

Complexity is O(N^3) where N is the size of the array. since we need combinations of 3 elements, i don't think you can go below that.

UPDATE: to make is more general, use a HashSet<Integer> for the sums instead of the boolean array, and iterate 3 nested loops over the input array. calculate the sum of squares and check against the HashSet.

Performance Optimization: calculate the squares of each element in the array in advance so you dont have to calculate them over and over again.

huangapple
  • 本文由 发表于 2020年8月6日 18:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定