0
0

More than 1 year has passed since last update.

paizaラーニング「部分配列 Python3編」

Posted at

私の解答

a, b, n = map(int, input().split())
li = [int(x) for x in input().split()]
for i in range(a-1, b):
    print(li[i])

私の解答は解答例2に近いです。

解答例

values = input().split()
A = int(values[0])
B = int(values[1])
N = int(values[2])

a = [0] * N
values = input().split()
for i in range(N):
    a[i] = int(values[i])

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

解答例2

A, B, N = map(int, input().split())
a = [int(x) for x in input().split()]

for ele in a[A-1:B]:
    print(ele)
  • スライスを用いた方法です。
  • a[A-1:B] のような記法を「スライス」と言います。
  • スライスはリストや文字列などに対して使うことができ、変数名[s:t] のように使うと、インデックスが s 以上 t 未満の要素をもったリストを取得することができます。
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