forked from lastwhispers/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
60 lines (55 loc) · 1.42 KB
/
Copy pathMain.java
File metadata and controls
60 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cn.cunchang;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
/**
* @author kaisui
* @description
* @date 2022/12/26
*/
public class Main {
static Map<Integer, Integer> map = new HashMap<>();
/**
* 判断数组中的数是否可以组成另一个数
* a*n+b*m=c
*/
public static void main(String[] args) {
// a*n+b*m=c
//1、n=1、m=1 || n=0||m==0
Integer[] arr = {50, 10};
int c = 60;
//2、n>1,m=1 || n=1,m>1
// Integer[] arr = {50, 10};
// int c = 110;
//
//3、n>1,m>1
// Integer[] arr = {50, 90};
// Integer[] arr = {50, 80};
// int c = 260;
boolean xxxx = xxxx(arr, c);
if (xxxx) {
System.out.println("找到了:" + JSON.toJSONString(map));
} else {
System.out.println("没找到");
}
}
private static boolean xxxx(Integer[] arr, int c) {
if (c == 0) {
return true;
}
if (c < 0) {
return false;
}
for (int i = 0; i < arr.length; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
c = c - arr[i];
if (xxxx(arr, c)) {
return true;
}
// 回溯
c = c + arr[i];
map.put(arr[i], map.get(arr[i]) - 1);
}
return false;
}
}