Skip to content

Commit 04c381e

Browse files
committed
chapter_12 finished
1 parent d11de63 commit 04c381e

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

chapter_17/coin_problem/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter_17/coin_problem/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "coin_problem"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

chapter_17/coin_problem/input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
15 6
2+
1 2 7 8 12 50

chapter_17/coin_problem/src/main.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn main() {
2+
let input_info: Vec<usize> = input::read_numline();
3+
let target_value = input_info[0];
4+
let coins: Vec<usize> = input::read_numline();
5+
6+
let mut coin_count: Vec<usize> = vec![100000000000000; target_value + 1];
7+
coin_count[0] = 0;
8+
for coin in coins {
9+
let mut try_value = 1;
10+
while try_value <= target_value {
11+
if 0 > try_value as isize - coin as isize {
12+
try_value += 1;
13+
continue;
14+
}
15+
if coin_count[try_value] > coin_count[try_value - coin] + 1 {
16+
coin_count[try_value] = coin_count[try_value - coin] + 1;
17+
}
18+
try_value += 1;
19+
}
20+
}
21+
println!("answer is {}", coin_count[target_value]);
22+
}

chapter_17/knapscack_problem/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "knapscack_problem"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#[derive(Clone, PartialEq)]
2+
enum Select {
3+
UnSelect,
4+
Select,
5+
}
6+
struct Object {
7+
value: usize,
8+
weight: usize,
9+
}
10+
11+
fn main() {
12+
let input_info: Vec<usize> = input::read_numline();
13+
let object_num = input_info[0] + 1;
14+
let max_weight = input_info[1];
15+
let mut object_info: Vec<Object> = vec![];
16+
let mut max_value: Vec<Vec<usize>> = vec![vec![0; max_weight + 1]; object_num];
17+
let mut select_object: Vec<Vec<Select>> = vec![vec![Select:: UnSelect; max_weight + 1]; object_num];
18+
19+
for _ in 0..input_info[0] {
20+
let input_object: Vec<usize> = input::read_numline();
21+
let object = Object{value: input_object[0], weight: input_object[1]};
22+
object_info.push(object);
23+
}
24+
25+
for object_index in 1..object_num {
26+
let object = &object_info[object_index - 1];
27+
for weight in 1..=max_weight {
28+
if weight < object.weight {
29+
max_value[object_index][weight] = max_value[object_index - 1][weight] ;
30+
continue;
31+
}
32+
if max_value[object_index - 1][weight] < max_value[object_index - 1][weight - object.weight] + object.value {
33+
max_value[object_index][weight] = max_value[object_index - 1][weight - object.weight] + object.value;
34+
select_object[object_index][weight] = Select:: Select;
35+
} else {
36+
max_value[object_index][weight] = max_value[object_index - 1][weight] ;
37+
}
38+
}
39+
}
40+
let mut serachindex = input_info[0];
41+
let mut search_weight = max_weight;
42+
43+
println!("max value is {}", max_value[object_num -1][max_weight]);
44+
45+
while serachindex >= 1 {
46+
if select_object[serachindex][search_weight] == Select:: Select {
47+
println!("object {} is selected", serachindex -1);
48+
search_weight -= object_info[serachindex -1].weight;
49+
}
50+
serachindex -= 1;
51+
}
52+
}

0 commit comments

Comments
 (0)