回答編集履歴
1
ソースを詳細に修正
answer
CHANGED
@@ -4,20 +4,31 @@
|
|
4
4
|
枝刈りという、その条件に合致しないパターンであれば探索を打ち切る仕組みが入りますが、
|
5
5
|
この例だとすべて列挙なので枝刈りも必要ないです
|
6
6
|
|
7
|
-
っ
|
7
|
+
ちょっと修正して、1が3つあるとき、最大2円で6が返ってくるのを確認しました
|
8
|
-
|
8
|
+
```
|
9
|
-
|
9
|
+
#include <stdio.h>
|
10
|
+
struct Data{
|
11
|
+
float price;
|
12
|
+
};
|
10
13
|
|
11
|
-
```
|
12
|
-
|
14
|
+
int pick(Data *data, int index, int maxindex, float price, float maxprice){
|
15
|
+
int count = 0;
|
13
|
-
if (maxindex <= index) return;
|
16
|
+
if (maxindex <= index) return 0;
|
14
17
|
float newprice = price + data[index].price;
|
15
18
|
if (newprice <= maxprice){
|
16
|
-
(
|
19
|
+
(count)++;
|
17
20
|
// indexの商品を追加して次の工程に
|
18
|
-
pick(data, index + 1, maxindex, newprice, maxprice
|
21
|
+
count += pick(data, index + 1, maxindex, newprice, maxprice);
|
19
22
|
}
|
20
23
|
// indexの商品を追加せずに次の工程に
|
21
|
-
pick(data, index + 1, maxindex, price, maxprice
|
24
|
+
count += pick(data, index + 1, maxindex, price, maxprice);
|
25
|
+
return count;
|
22
26
|
}
|
27
|
+
|
28
|
+
int main(){
|
29
|
+
Data d[] = {{1}, {1}, {1}};
|
30
|
+
|
31
|
+
int count = pick(d, 0, 3, 0, 2);
|
32
|
+
printf("%d", count);
|
33
|
+
}
|
23
34
|
```
|