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 all 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
12 changes: 8 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,8 @@ int main() {
int n;
cin >> n;
int second_latest = 0, latest = 1;
for (int i = 2; i < n; i++) {
for (int i = 2; i <= n; i++) {
// i番目を計算
int next = second_latest + latest;
second_latest = latest;
latest = next;
Expand All @@ -44,11 +47,12 @@ int main() {
int n;
cin >> n;
vector<int> fibonatti_sequence = {0, 1};
for (int i = 2; 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