Skip to content

Commit

Permalink
5章の練習問題 (#9)
Browse files Browse the repository at this point in the history
* add practices

* edit config.ts

* "+=" is difficulty to pronounce

* bits is not allowed

* add practice "exponentation"

* fix 日本語

* delete practice "Exponantation"

* add hint to divide-each-diff

* line-numbers

* deleted exponentation from index.md
  • Loading branch information
comavius authored May 7, 2024
1 parent bd272b7 commit 5d02a42
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export default withMermaid({
{ text: '引数', link: '/text/chapter-5/argument' },
{ text: '返り値', link: '/text/chapter-5/return-value' },
{ text: '参照渡し', link: '/text/chapter-5/call-by-ref' },
{text: '練習問題', link: '/text/chapter-5/practice/'}
]
},
{
Expand Down
83 changes: 83 additions & 0 deletions docs/text/chapter-5/practice/divide-each-difficulty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Divide each difficulty
以下の$n \leq 7$を受け取って$n \times n$の行列$a_{i,j}=Fibonatti_{ij}$を出力するプログラムの、フィボナッチ数列の解を求める部分を別の関数`int fibonatti(int index);`に分離してみよう。
```cpp:line-numbers
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// ここから下を関数に切り分ける
// print (i*j)th of fibonatti sequence
int first = 1, second = 1;
for (int k = 0; k < i*j; k++) {
int next = first + second;
first = second;
second = next;
}
// ここから上を関数に切り分ける
cout << second << " ";
}
cout << endl;
}
}
```

::: spoiler Hint
下のコードの`// ここにフィボナッチ数列の計算を実装しよう`の部分を実装してみよう。
```cpp:line-numbers
#include <iostream>
using namespace std;
int fibonatti(int index) {
int first = 1, second = 1;
// ここにフィボナッチ数列の計算を実装しよう
return second;
}
int main() {
int n;
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int fib_ij = fibonatti(i*j);
cout << fib_ij << " ";
}
cout << endl;
}
}
```
:::

::: spoiler Answer

```cpp:line-numbers
#include <iostream>
using namespace std;
int fibonatti(int index) {
int first = 1, second = 1;
for (k = 0; k < index; k++) {
int next = first + second;
first = second;
second = next;
}
return second;
}
int main() {
int n;
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int fib_ij = fibonatti(i*j);
cout << fib_ij << " ";
}
cout << endl;
}
}
```

:::
4 changes: 4 additions & 0 deletions docs/text/chapter-5/practice/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 練習問題 - Chapter 5

- [Divide each difficulty](divide-each-difficulty)
- [Operator+=](plus-equal)
21 changes: 21 additions & 0 deletions docs/text/chapter-5/practice/plus-equal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Operator+=
`int`型の`a``b`について`a``b`を足す操作である

```cpp
a += b;
```

で用いる`+=`演算子と同じ働きをする関数を書こう。

::: spoiler Hint 1
`a`を参照渡しで受け取ることで、`a`の値を書き換えることができる。
:::

::: spoiler Answer
```cpp:line-numbers
void compound_assigned_plus(int& lhs, int rhs) {
lhs = lhs + rhs;
return;
}
```
:::

0 comments on commit 5d02a42

Please sign in to comment.