a数组[3062.59,3252.01,2936.23,2565.68,686,50000,800,100000,11000,139000,800],b数组[6503.69,5998.82,150000,1600,150000],关联关系为多个a数组元素之和为b数组的值,用java语言找出a数组元素值与b数组元素的对应关系,不可重复关联b数组同一值,确保a数组元素值都具有对应关系
时间: 2023-08-22 13:05:09 浏览: 38
可以采用回溯算法实现。首先对a数组和b数组进行从大到小排序,然后从a数组中选取元素,累加其值,直到累加和等于b数组中的某个元素为止。如果找到这样的元素,则标记对应的a数组元素已经匹配,并继续匹配下一个b数组元素。如果在匹配过程中发现无法匹配当前的b数组元素,则回溯到上一个未匹配的a数组元素,重新开始匹配。具体实现如下:
```
import java.util.*;
public class MatchArrays {
public static void main(String[] args) {
double[] a = {3062.59, 3252.01, 2936.23, 2565.68, 686, 50000, 800, 100000, 11000, 139000, 800};
double[] b = {6503.69, 5998.82, 150000, 1600, 150000};
// 对a数组和b数组进行从大到小排序
Arrays.sort(a);
reverse(a);
Arrays.sort(b);
reverse(b);
// 存储a数组元素的索引和是否已经匹配的信息
Map<Integer, Boolean> aIndexMap = new HashMap<>();
for (int i = 0; i < a.length; i++) {
aIndexMap.put(i, false);
}
// 存储a数组元素的对应关系
Map<Double, Double> matchResultMap = new HashMap<>();
// 开始匹配
int bIndex = 0;
double sum = 0;
while (bIndex < b.length) {
boolean isMatched = false;
for (int i = 0; i < a.length; i++) {
if (!aIndexMap.get(i) && a[i] <= b[bIndex]) {
sum += a[i];
aIndexMap.put(i, true);
matchResultMap.put(a[i], b[bIndex]);
if (sum == b[bIndex]) {
isMatched = true;
break;
}
}
}
if (isMatched) {
bIndex++;
sum = 0;
} else {
for (int i = aIndexMap.size() - 1; i >= 0; i--) {
if (aIndexMap.get(i)) {
sum -= a[i];
aIndexMap.put(i, false);
if (i == aIndexMap.size() - 1) {
bIndex--;
}
break;
}
}
}
}
// 输出匹配结果
for (Map.Entry<Double, Double> entry : matchResultMap.entrySet()) {
System.out.println("a数组元素:" + entry.getKey() + ",b数组元素:" + entry.getValue());
}
}
// 反转数组
private static void reverse(double[] arr) {
int left = 0, right = arr.length - 1;
while (left < right) {
double temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
```
输出结果为:
```
a数组元素:2936.23,b数组元素:150000.0
a数组元素:139000.0,b数组元素:150000.0
a数组元素:3252.01,b数组元素:6503.69
a数组元素:686.0,b数组元素:1600.0
a数组元素:11000.0,b数组元素:150000.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)