英文:
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 = {"1", "2", "3","4"};
int count = fuctorial(arr.length);
int max = arr.length - 1;
System.out.println("Вариантов " + 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;
}
答案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 < 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);
}
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 <=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);
}
}
}
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论