gpt4 book ai didi

java - 两个整数数组之间的计算

转载 作者:行者123 更新时间:2023-11-29 04:58:17 26 4
gpt4 key购买 nike

我有一个随机生成的整数数组(A1),其中包括以下特征。

  1. 数组长度为4
  2. 元素填充了从 1 到 9 的整数值
  3. 没有重复值
  4. 升序排列

现在有另一个数组(A2),它是A1的降序数组。
我打算做的是,在这两个数组之间进行所有可能的线性和交叉计算(仅相加)。
起始数组可以是 A1 或 A2,但应始终从第零个元素开始。
A1 或 A2 中的值,可以取正数或负数形式来进行计算。
值应按数组索引的递增顺序从数组中选取。

示例:1 ex1

示例:2 ex2

示例:3
ex3

示例:4
ex4

起初,我一直试图通过仅使用 for 循环来实现这一点,但后来我对其应用了递归。但是,它只适用于计算一个或两个值。有什么有效的方法可以做到这一点?希望我的问题足够清楚!

已编辑

让我们取 A1=[1,2,3,4] 那么,A2=[4,3,2,1]
那么可能的计算结果应该是这样的

  1. 1 + 无 = 1
  2. -1 + 无 = -1
  3. 4 + 无 = 4
  4. -4 + 无 = -4
  5. 1 + 2 = 3
  6. 1 - 2 = -1
  7. 1 + 3 = 4
  8. 1 - 3 = -2
  9. -1 + 2 = 1
  10. -1 - 2 = -3
  11. -1 + 3 = 2
  12. -1 - 3 = -4
  13. 1 + 2 + 3 = 6
  14. ……
  15. ……

最后,对于长度为 4 的数组,应该有 340 种可能的计算。

1, -1, 4, -4, 3, -1, 4, -2, 1, -3,
2, -4, 6, 2, 7, 1, -2, -6, -1, -7,
6, 0, 5, 1, 2, -4, 1, -3, 7, 1,
6, 2, 1, -5, 0, -4, 4, -2, 3, -1,
0, -6, -1, -5, 5, -1, 4, 0, -1, -7,
-2, -6, 9, 3, 8, 4, 5, -1, 4, 0,
10, 4, 9, 5, 4, -2, 3, -1, 1, -5,
0, -4, -3, -9, -4, -8, 2, -4, 1, -3,
-4, -10, -5, -9, 10, 2, 7, 5, 4, -4,
1, -1, 9, 1, 6, 4, 5, -3, 2, 0,
6, -2, 3, 1, 0, -8, -3, -5, 5, -3,
2, 0, 1, -7, -2, -4, 11, 3, 8, 6,
5, -3, 2, 0, 10, 2, 7, 5, 6, -2,
3, 1, 5, -3, 2, 0, -1, -9, -4, -6,
4, -4, 1, -1, 0, -8, -3, -5, 8, 0,
5, 3, 2, -6, -1, -3, 7, -1, 4, 2,
3, -5, 0, -2, 4, -4, 1, -1, -2, -10,
-5, -7, 3, -5, 0, -2, -1, -9, -4, -6,
9, 1, 6, 4, 3, -5, 0, -2, 8, 0,
5, 3, 4, -4, 1, -1, 3, -5, 0, -2,
-3, -11, -6, -8, 2, -6, -1, -3, -2, -10,
-5, -7, 13, 5, 10, 8, 7, -1, 4, 2,
12, 4, 9, 7, 8, 0, 5, 3, 9, 1,
6, 4, 3, -5, 0, -2, 8, 0, 5, 3,
4, -4, 1, -1, 14, 6, 11, 9, 8, 0,
5, 3, 13, 5, 10, 8, 9, 1, 6, 4,
8, 0, 5, 3, 2, -6, -1, -3, 7, -1,
4, 2, 3, -5, 0, -2, 5, -3, 2, 0,
-1, -9, -4, -6, 4, -4, 1, -1, 0, -8,
-3, -5, 1, -7, -2, -4, -5, -13, -8, -10,
0, -8, -3, -5, -4, -12, -7, -9, 6, -2,
3, 1, 0, -8, -3, -5, 5, -3, 2, 0,
1, -7, -2, -4, 0, -8, -3, -5, -6, -14,
-9, -11, -1, -9, -4, -6, -5, -13, -8, -10,

最佳答案

是这样的吗?

public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println(Arrays.toString(test.A1));
System.out.println(Arrays.toString(test.A2));
System.out.println(test.result);
}
private int[] A1, A2;
private List<Integer> result;
private Test() {
List<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9));
Random random = new Random();
this.A1 = new int[4];
for (int i = 0; i < this.A1.length; i++) {
int idx = random.nextInt(digits.size());
this.A1[i] = digits.remove(idx);
}
Arrays.sort(this.A1);

this.A2 = new int[this.A1.length];
for (int i = 0; i < this.A1.length; i++)
this.A2[i] = this.A1[this.A1.length - i - 1];

this.result = new ArrayList<>();
for (int i = 0; i < this.A1.length; i++)
calc(i, 0);
}
private void calc(int idx, int sum) {
add(sum + this.A1[idx], idx + 1);
add(sum - this.A1[idx], idx + 1);
add(sum + this.A2[idx], idx + 1);
add(sum - this.A2[idx], idx + 1);
}
private void add(int sum, int nextIdx) {
this.result.add(sum);
if (nextIdx < this.A1.length)
calc(nextIdx, sum);
}
}

输出

[1, 2, 4, 7]
[7, 4, 2, 1]
[1, 3, 7, 14, 0, 8, 6, -1, 6, -8, 0, -2, 5, 12, -2, 6, 4, 1, 8, -6, 2, 0, -1, 3, 10, -4, 4, 2, -5, 2, -12, -4, -6, 1, 8, -6, 2, 0, -3, 4, -10, -2, -4, 5, 9, 16, 2, 10, 8, 1, 8, -6, 2, 0, 7, 14, 0, 8, 6, 3, 10, -4, 4, 2, -3, 1, 8, -6, 2, 0, -7, 0, -14, -6, -8, -1, 6, -8, 0, -2, -5, 2, -12, -4, -6, -1, 1, 5, 12, -2, 6, 4, -3, 4, -10, -2, -4, 3, 10, -4, 4, 2, -1, 6, -8, 0, -2, -3, 1, 8, -6, 2, 0, -7, 0, -14, -6, -8, -1, 6, -8, 0, -2, -5, 2, -12, -4, -6, 3, 7, 14, 0, 8, 6, -1, 6, -8, 0, -2, 5, 12, -2, 6, 4, 1, 8, -6, 2, 0, -5, -1, 6, -8, 0, -2, -9, -2, -16, -8, -10, -3, 4, -10, -2, -4, -7, 0, -14, -6, -8, 7, 9, 13, 20, 6, 14, 12, 5, 12, -2, 6, 4, 11, 18, 4, 12, 10, 7, 14, 0, 8, 6, 5, 9, 16, 2, 10, 8, 1, 8, -6, 2, 0, 7, 14, 0, 8, 6, 3, 10, -4, 4, 2, 11, 15, 22, 8, 16, 14, 7, 14, 0, 8, 6, 13, 20, 6, 14, 12, 9, 16, 2, 10, 8, 3, 7, 14, 0, 8, 6, -1, 6, -8, 0, -2, 5, 12, -2, 6, 4, 1, 8, -6, 2, 0, -7, -5, -1, 6, -8, 0, -2, -9, -2, -16, -8, -10, -3, 4, -10, -2, -4, -7, 0, -14, -6, -8, -9, -5, 2, -12, -4, -6, -13, -6, -20, -12, -14, -7, 0, -14, -6, -8, -11, -4, -18, -10, -12, -3, 1, 8, -6, 2, 0, -7, 0, -14, -6, -8, -1, 6, -8, 0, -2, -5, 2, -12, -4, -6, -11, -7, 0, -14, -6, -8, -15, -8, -22, -14, -16, -9, -2, -16, -8, -10, -13, -6, -20, -12, -14, 2, 6, 13, -1, 7, 5, -2, 5, -9, -1, -3, 4, 11, -3, 5, 3, 0, 7, -7, 1, -1, -2, 2, 9, -5, 3, 1, -6, 1, -13, -5, -7, 0, 7, -7, 1, -1, -4, 3, -11, -3, -5, 4, 8, 15, 1, 9, 7, 0, 7, -7, 1, -1, 6, 13, -1, 7, 5, 2, 9, -5, 3, 1, -4, 0, 7, -7, 1, -1, -8, -1, -15, -7, -9, -2, 5, -9, -1, -3, -6, 1, -13, -5, -7, 4, 11, -3, 5, 3, -4, 3, -11, -3, -5, 2, 9, -5, 3, 1, -2, 5, -9, -1, -3, 7, -7, 1, -1]

更新

由于您表示您希望公式的编号和打印,这里是一个非递归实现,它使用位模式选择每个 1-of-4 部分进行总结。

请注意,以前的实现和这个新的实现都返回 448 组合,而不是您提到的 340。

代码构建了所有组合的列表。然后,您可以选择对列表进行排序(像这样按组合长度排序),以及是否要收集总和,或者打印文本公式或数字公式。

public class Combo {
public static void main(String[] args) {
int[] A1 = { 1, 2, 3, 4 };
int[] A2 = { 4, 3, 2, 1 };
List<Combo> comboList = new ArrayList<>();
for (int start = 0; start < A1.length; start++)
for (int end = start; end < A1.length; end++) {
final int combinations = 1 << ((end - start + 1) << 1); // 4 ^ (end - start + 1)
for (int combo = 0; combo < combinations; combo++)
comboList.add(new Combo(start, end, combo));
}
Collections.sort(comboList, (c1, c2) -> Integer.compare(c1.end - c1.start, c2.end - c2.start));
for (int i = 0; i < comboList.size(); i++) {
Combo combo = comboList.get(i);
System.out.printf("%3d: %-33s = %-17s = %d%n", i + 1, combo.getTextFormula(),
combo.getNumberFormula(A1, A2), combo.getSum(A1, A2));
}
}
private final int start;
private final int end;
private final int combo;
public Combo(int start, int end, int combo) {
this.start = start;
this.end = end;
this.combo = combo;
}
private String getTextFormula() {
StringBuilder buf = new StringBuilder();
for (int i = this.start; i <= this.end; i++) {
int c = this.combo >> ((this.end - i) << 1) & 3;
if (i != this.start) buf.append(" + ");
buf.append(c == 0 ? "A1" : c == 1 ? "-A1" : c == 2 ? "A2" : "-A2").append('[').append(i).append(']');
}
return buf.toString();
}
private String getNumberFormula(int[] A1, int[] A2) {
StringBuilder buf = new StringBuilder();
for (int i = this.start; i <= this.end; i++) {
int c = this.combo >> ((this.end - i) << 1) & 3;
if (i != this.start) buf.append(" + ");
buf.append(c == 0 ? A1[i] : c == 1 ? -A1[i] : c == 2 ? A2[i] : -A2[i]);
}
return buf.toString();
}
private int getSum(int[] A1, int[] A2) {
int sum = 0;
for (int i = this.start; i <= this.end; i++) {
int c = this.combo >> ((this.end - i) << 1) & 3;
sum += (c == 0 ? A1[i] : c == 1 ? -A1[i] : c == 2 ? A2[i] : -A2[i]);
}
return sum;
}
}

输出

  1: A1[0]                             = 1                 = 1
2: -A1[0] = -1 = -1
3: A2[0] = 4 = 4
4: -A2[0] = -4 = -4
5: A1[1] = 2 = 2
6: -A1[1] = -2 = -2
7: A2[1] = 3 = 3
8: -A2[1] = -3 = -3
9: A1[2] = 3 = 3
10: -A1[2] = -3 = -3
11: A2[2] = 2 = 2
12: -A2[2] = -2 = -2
13: A1[3] = 4 = 4
14: -A1[3] = -4 = -4
15: A2[3] = 1 = 1
16: -A2[3] = -1 = -1
17: A1[0] + A1[1] = 1 + 2 = 3
18: A1[0] + -A1[1] = 1 + -2 = -1
. . .
63: -A2[2] + A2[3] = -2 + 1 = -1
64: -A2[2] + -A2[3] = -2 + -1 = -3
65: A1[0] + A1[1] + A1[2] = 1 + 2 + 3 = 6
66: A1[0] + A1[1] + -A1[2] = 1 + 2 + -3 = 0
. . .
127: -A2[0] + -A2[1] + A2[2] = -4 + -3 + 2 = -5
128: -A2[0] + -A2[1] + -A2[2] = -4 + -3 + -2 = -9
129: A1[1] + A1[2] + A1[3] = 2 + 3 + 4 = 9
130: A1[1] + A1[2] + -A1[3] = 2 + 3 + -4 = 1
. . .
191: -A2[1] + -A2[2] + A2[3] = -3 + -2 + 1 = -4
192: -A2[1] + -A2[2] + -A2[3] = -3 + -2 + -1 = -6
193: A1[0] + A1[1] + A1[2] + A1[3] = 1 + 2 + 3 + 4 = 10
194: A1[0] + A1[1] + A1[2] + -A1[3] = 1 + 2 + 3 + -4 = 2
. . .
255: A1[0] + -A2[1] + -A2[2] + A2[3] = 1 + -3 + -2 + 1 = -3
256: A1[0] + -A2[1] + -A2[2] + -A2[3] = 1 + -3 + -2 + -1 = -5
257: -A1[0] + A1[1] + A1[2] + A1[3] = -1 + 2 + 3 + 4 = 8
258: -A1[0] + A1[1] + A1[2] + -A1[3] = -1 + 2 + 3 + -4 = 0
. . .
319: -A1[0] + -A2[1] + -A2[2] + A2[3] = -1 + -3 + -2 + 1 = -5
320: -A1[0] + -A2[1] + -A2[2] + -A2[3] = -1 + -3 + -2 + -1 = -7
321: A2[0] + A1[1] + A1[2] + A1[3] = 4 + 2 + 3 + 4 = 13
322: A2[0] + A1[1] + A1[2] + -A1[3] = 4 + 2 + 3 + -4 = 5
. . .
383: A2[0] + -A2[1] + -A2[2] + A2[3] = 4 + -3 + -2 + 1 = 0
384: A2[0] + -A2[1] + -A2[2] + -A2[3] = 4 + -3 + -2 + -1 = -2
385: -A2[0] + A1[1] + A1[2] + A1[3] = -4 + 2 + 3 + 4 = 5
386: -A2[0] + A1[1] + A1[2] + -A1[3] = -4 + 2 + 3 + -4 = -3
. . .
440: -A2[0] + -A2[1] + -A1[2] + -A2[3] = -4 + -3 + -3 + -1 = -11
441: -A2[0] + -A2[1] + A2[2] + A1[3] = -4 + -3 + 2 + 4 = -1
442: -A2[0] + -A2[1] + A2[2] + -A1[3] = -4 + -3 + 2 + -4 = -9
443: -A2[0] + -A2[1] + A2[2] + A2[3] = -4 + -3 + 2 + 1 = -4
444: -A2[0] + -A2[1] + A2[2] + -A2[3] = -4 + -3 + 2 + -1 = -6
445: -A2[0] + -A2[1] + -A2[2] + A1[3] = -4 + -3 + -2 + 4 = -5
446: -A2[0] + -A2[1] + -A2[2] + -A1[3] = -4 + -3 + -2 + -4 = -13
447: -A2[0] + -A2[1] + -A2[2] + A2[3] = -4 + -3 + -2 + 1 = -8
448: -A2[0] + -A2[1] + -A2[2] + -A2[3] = -4 + -3 + -2 + -1 = -10

关于java - 两个整数数组之间的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com