Skip to content

Commit b39c8bf

Browse files
committed
Finished Chaper 3
1 parent 6643739 commit b39c8bf

File tree

13 files changed

+267
-3
lines changed

13 files changed

+267
-3
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'problem_1'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=problem_1",
15+
"--package=problem_1"
16+
],
17+
"filter": {
18+
"name": "problem_1",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'problem_1'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=problem_1",
34+
"--package=problem_1"
35+
],
36+
"filter": {
37+
"name": "problem_1",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

chapter_3/problem_1/src/main.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
use input::read_number;
22

33
fn main() {
4-
let num: i32 = read_number();
5-
println!("Hello, world!");
4+
let size: i32 = read_number();
5+
let mut nums: Vec<i32> = input::read_numline();
6+
for i in 1..size {
7+
let value = nums[i as usize];
8+
let mut j = i - 1;
9+
while j >= 0 && nums[j as usize] > value {
10+
nums.swap(j as usize, (j + 1) as usize);
11+
j -= 1;
12+
}
13+
}
14+
println!("order is {:?}", nums);
615
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'problem_2'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=problem_2",
15+
"--package=problem_2"
16+
],
17+
"filter": {
18+
"name": "problem_2",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'problem_2'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=problem_2",
34+
"--package=problem_2"
35+
],
36+
"filter": {
37+
"name": "problem_2",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

chapter_3/problem_2/Cargo.lock

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

chapter_3/problem_2/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
input = {path = "../../library/input"}

chapter_3/problem_2/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
fn main() {
2-
println!("Hello, world!");
2+
let size: usize = input::read_number();
3+
let mut nums: Vec<i32> = input::read_numline();
4+
let mut do_sort = true;
5+
let mut sord_finished_index = 1;
6+
while do_sort {
7+
do_sort = false;
8+
for i in (sord_finished_index..size).rev() {
9+
if nums[i] < nums[i-1] {
10+
do_sort = true;
11+
nums.swap(i, i-1);
12+
}
13+
}
14+
sord_finished_index += 1;
15+
}
16+
println!("order is {:?}", nums);
317
}

chapter_3/problem_3/Cargo.lock

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

chapter_3/problem_3/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_3"
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_3/problem_3/src/main.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn main() {
2+
let size: i32 = input::read_number();
3+
let mut nums: Vec<i32> = input::read_numline();
4+
for i in 0..size {
5+
let mut min_index = i;
6+
let mut min_value = nums[i as usize];
7+
for j in i..size {
8+
if nums[j as usize] < min_value {
9+
min_index = j;
10+
min_value = nums[j as usize];
11+
}
12+
}
13+
nums.swap(i as usize, min_index as usize);
14+
}
15+
println!("order is {:?}", nums);
16+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'problem_4'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=problem_4",
15+
"--package=problem_4"
16+
],
17+
"filter": {
18+
"name": "problem_4",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'problem_4'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=problem_4",
34+
"--package=problem_4"
35+
],
36+
"filter": {
37+
"name": "problem_4",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

chapter_3/problem_4/Cargo.lock

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

chapter_3/problem_4/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_4"
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_3/problem_4/src/main.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fn make_shell_vec(max_size: i32) -> Vec<i32> {
2+
let mut shell_vec: Vec<i32> = Vec::new();
3+
let mut shell = 1;
4+
loop {
5+
if shell > max_size {
6+
break;
7+
}
8+
shell_vec.push(shell);
9+
shell = 3*shell + 1;
10+
}
11+
shell_vec
12+
}
13+
14+
fn shell_sort(vec: &mut Vec<i32>, shell: i32, size: i32) {
15+
for mut index in shell..size{
16+
let value = vec[index as usize];
17+
while index >= shell && vec[(index - shell) as usize] > value{
18+
vec.swap((index - shell) as usize, index as usize);
19+
index -= shell;
20+
}
21+
22+
}
23+
}
24+
25+
fn main() {
26+
let size: i32 = input::read_number();
27+
let mut nums: Vec<i32> = input::read_numline();
28+
let shell_vec = make_shell_vec(size);
29+
for shell in shell_vec.into_iter().rev() {
30+
shell_sort(&mut nums, shell, size);
31+
}
32+
println!(" order is {:?}", nums);
33+
}

0 commit comments

Comments
 (0)