1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

フィボナッチ数 (paizaランク D 相当)

Posted at

フィボナッチ数列をプログラムで表現する問題。
1番目は0,2番めは1,3番目以降は1番目と2番めの数の和・・・というやつ
そのままプログラミングした

N = int(input())
ans = 0
num1 = 0
num2 = 0
for i in range(N):
    if i + 1 == 1:
        ans = 0
        num1 = 0
        print(ans)
    if i + 1 == 2:
        ans = 1
        num2= 1
        print(ans)
    if i + 1 > 2:
        ans = num1 + num2
        print(ans)
        num1 = num2
        num2 = ans

下手くそなプログラミングだなぁ。。。
答えを見てみた。
配列をうまく使っていた。
なので書き直し

N = int(input())

A = [0] * N
A[0] = 0
A[1] = 1

for i in range(2, N):
    A[i] = A[i - 2] + A[i - 1]

print(*A,sep="\n")

今日はここまでで。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?