Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第4章の練習問題「Fibonatti Number」を修正 #29

Merged
merged 6 commits into from
May 30, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/text/chapter-4/practice/fibonatti.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 4-xx. Fibonatti Number

整数$N$を受け取り、フィボナッチ数列の$N$番目を出力しよう。
正の整数$N$を受け取り、フィボナッチ数列の$N$番目を出力しよう。

ただし、フィボナッチ数列は $\{1,1,2,...\}$ とします。

:::spoiler Hint 1
$F_{n}=F_{n-1}+F_{n-2}$をfor文で計算しよう。
Expand All @@ -24,7 +26,7 @@ int main() {
int n;
cin >> n;
int second_latest = 0, latest = 1;
for (int i = 2; i < n; i++) {
for (int i = 1; i < n; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回数のためにしか使ってないなら

Suggested change
for (int i = 1; i < n; i++) {
for (int i = 0; i < n-1; i++) {

$i$ 番目を計算するという目的にしたいなら

Suggested change
for (int i = 1; i < n; i++) {
for (int i = 2; i <= n; i++) {

としたいです

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返信遅くなりすみません.

i番目を計算するほうが分かりやすいと思うので, その旨のコメントを追加して以下でどうでしょうか.

Suggested change
for (int i = 1; i < n; i++) {
for (int i = 2; i <= n; i++) {
// i番目を計算

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良さげ!コミットしちゃってください

int next = second_latest + latest;
second_latest = latest;
latest = next;
Expand All @@ -44,11 +46,11 @@ int main() {
int n;
cin >> n;
vector<int> fibonatti_sequence = {0, 1};
for (int i = 2; i < n; i++) {
for (int i = 1; i < n; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同様

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上と同様です.

Suggested change
for (int i = 1; i < n; i++) {
for (int i = 2; i <= n; i++) {
// i番目を計算

int next = fibonatti_sequence[i-1] + fibonatti_sequence[i-2];
fibonatti_sequence.push_back(next);
}
cout << fibonatti_sequence.back() << endl;
}

```
:::
Loading