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 の数列 A と要素数 Q の数列 B が与えられます。 1 ≦ i ≦ Q の各 i について、i 行目に A の B_i 番目の値を出力してください。

という問題。
さっそく覚えた内包表記をつかってAとBの配列を作ってごにょごにょやる(言い方)

N = int(input())
A =[int(x) for x in input().split()]
Q = int(input())
B =[int(y) for y in input().split()]
for i in B:
    print(A[i-1])

Qをつかうならその次のfor文はこんな感じに。
わかりやすいのはどっちかな? 多分上?

for i in range(Q):
    print(A[B[i]-1])

内包表記を使うときれいに1行にまとまるね〜。

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?