-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask12.java
58 lines (43 loc) · 1.22 KB
/
Task12.java
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
import java.io.*;
import java.util.*;
public class Task12 {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int sum = in.nextInt();
//System.out.println(sum);
List<Integer> elements = new ArrayList<Integer>();
String[] e= in.next().split(",");
for (String el: e){
elements.add(Integer.parseInt(el));
}
Collections.sort(elements);
//System.out.println(elements);
go(0, sum, elements, new ArrayList<Integer>());
System.out.println(done.size());
}
static Set<String> done = new HashSet<String>();
public static void go(int now, int target, List<Integer> er, List<Integer> en){
if (now == target){
String sum = "";
for (int i = 0; i < en.size(); i++){
sum += en.get(i);
if (i != en.size()-1){
sum += "+";
}
}
if (!done.contains(sum)){
done.add(sum);
}
}
if (now > target){
return;
}
for (int e: er){
List<Integer> eRC = new ArrayList<Integer>(er);
List<Integer> eNC = new ArrayList<Integer>(en);
eRC.remove(new Integer(e));
eNC.add(e);
go(now + e, target, eRC, eNC);
}
}
}