File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -128,6 +128,7 @@ export default withMermaid({
128128 { text : '引数' , link : '/text/chapter-5/argument' } ,
129129 { text : '返り値' , link : '/text/chapter-5/return-value' } ,
130130 { text : '参照渡し' , link : '/text/chapter-5/call-by-ref' } ,
131+ { text : '練習問題' , link : '/text/chapter-5/practice/' }
131132 ]
132133 } ,
133134 {
Original file line number Diff line number Diff line change 1+ # Divide each difficulty
2+ 以下の$n \leq 7$を受け取って$n \times n$の行列$a_ {i,j}=Fibonatti_ {ij}$を出力するプログラムの、フィボナッチ数列の解を求める部分を別の関数` int fibonatti(int index); ` に分離してみよう。
3+ ``` cpp:line-numbers
4+ #include <iostream>
5+ using namespace std;
6+
7+ int main() {
8+ int n;
9+ cin >> n;
10+ for (int i = 0; i < n; i++) {
11+ for (int j = 0; j < n; j++) {
12+ // ここから下を関数に切り分ける
13+ // print (i*j)th of fibonatti sequence
14+ int first = 1, second = 1;
15+ for (int k = 0; k < i*j; k++) {
16+ int next = first + second;
17+ first = second;
18+ second = next;
19+ }
20+ // ここから上を関数に切り分ける
21+ cout << second << " ";
22+ }
23+ cout << endl;
24+ }
25+ }
26+ ```
27+
28+ ::: spoiler Hint
29+ 下のコードの` // ここにフィボナッチ数列の計算を実装しよう ` の部分を実装してみよう。
30+ ``` cpp:line-numbers
31+ #include <iostream>
32+ using namespace std;
33+
34+ int fibonatti(int index) {
35+ int first = 1, second = 1;
36+ // ここにフィボナッチ数列の計算を実装しよう
37+ return second;
38+ }
39+
40+ int main() {
41+ int n;
42+ cin >> n;
43+ for (i = 0; i < n; i++) {
44+ for (j = 0; j < n; j++) {
45+ int fib_ij = fibonatti(i*j);
46+ cout << fib_ij << " ";
47+ }
48+ cout << endl;
49+ }
50+ }
51+ ```
52+ :::
53+
54+ ::: spoiler Answer
55+
56+ ``` cpp:line-numbers
57+ #include <iostream>
58+ using namespace std;
59+
60+ int fibonatti(int index) {
61+ int first = 1, second = 1;
62+ for (k = 0; k < index; k++) {
63+ int next = first + second;
64+ first = second;
65+ second = next;
66+ }
67+ return second;
68+ }
69+
70+ int main() {
71+ int n;
72+ cin >> n;
73+ for (i = 0; i < n; i++) {
74+ for (j = 0; j < n; j++) {
75+ int fib_ij = fibonatti(i*j);
76+ cout << fib_ij << " ";
77+ }
78+ cout << endl;
79+ }
80+ }
81+ ```
82+
83+ :::
Original file line number Diff line number Diff line change 1+ # 練習問題 - Chapter 5
2+
3+ - [ Divide each difficulty] ( divide-each-difficulty )
4+ - [ Operator+=] ( plus-equal )
Original file line number Diff line number Diff line change 1+ # Operator+=
2+ ` int ` 型の` a ` と` b ` について` a ` に` b ` を足す操作である
3+
4+ ``` cpp
5+ a += b;
6+ ```
7+
8+ で用いる` += ` 演算子と同じ働きをする関数を書こう。
9+
10+ ::: spoiler Hint 1
11+ ` a ` を参照渡しで受け取ることで、` a ` の値を書き換えることができる。
12+ :::
13+
14+ ::: spoiler Answer
15+ ``` cpp:line-numbers
16+ void compound_assigned_plus(int& lhs, int rhs) {
17+ lhs = lhs + rhs;
18+ return;
19+ }
20+ ```
21+ :::
You can’t perform that action at this time.
0 commit comments