-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path574.cpp
76 lines (62 loc) · 1.3 KB
/
574.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <cstdio>
#include <cstring>
using namespace std;
struct element {
int v, n, sn;
} list[20];
bool exist, first;
int goal, len;
void show()
{
int i, j;
first = true;
for (i = 0; i < len; ++i) {
for (j = 0; j < list[i].sn; ++j)
if (!first)
printf("+%d", list[i].v);
else {
printf("%d", list[i].v);
first = false;
}
}
printf("\n");
}
void match(int index, int sum)
{
int i;
if (sum == goal) {
show();
exist = true;
return;
}
else if (index == len)
return;
for (i = list[index].n; i > -1; --i) {
list[index].sn = i;
match(index + 1, sum + i * list[index].v);
}
}
int main(void)
{
int i;
while (true) {
scanf("%d %d", &goal, &i);
if (!goal && !i) break;
len = 0;
while (i--) {
scanf("%d", &list[len].v);
if (len == 0 || list[len].v != list[len - 1].v) {
list[len].n = 1;
list[len].sn = 0;
len++;
}
else
list[len - 1].n++;
}
exist = false;
printf("Sums of %d:\n", goal);
match(0, 0);
if (!exist) printf("NONE\n");
}
return 0;
}