0
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?

配列の順序の反転 Python3編

Posted at

私の答えは

N = int(input())
A = [int(input()) for _ in range(N)]
print(*reversed(A),sep="\n")

まあ、これは前にもやったことなので難しくはない。
仮に、reversed関数がなかったとすると、どうするか。
まずはA配列を作るところまでは同じで
逆順のB配列を作るにはどうするか。
とりあえず前勉強した通り空の配列を作る

B = [None] * N #0でも同じ結果が出る

たとえば、5つの要素があるとして
B[0]=A[4]
B[1]=A[3]
B[2]=A[2]
.
.
.
という順番になる。
これをルール化すればいいわけで、
i = 0なら、NからN(=ここでは5)-0(=i)-1
とすればいいのではと考えて、

N = int(input())
A = [int(input()) for _ in range(N)]
B = [None] * N
for i in range(len(A)):
    B[i] = A[N-i-1]

print(*B,sep='\n')

OKでした

0
0
0

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
0
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?