1
2

More than 3 years have passed since last update.

競プロで役立つPython3の入力まとめ

Last updated at Posted at 2020-04-26

数値

単一の数値

I = int(input())

空白区切りでの入力

3 5 6 7

のような数値が空白区切りで入力される際

A, B, C, D = map(int, input().split())

改行区切りでの入力

3
5
6
7

のような数値が改行区切りで入力される際

A, B, C, D = map(int,open(0))

数値を受け取る数が未定かつリストにしたいとき

例えば入力が

3 5 6 7 ,,, N

の様な時以下の様なlistを作成したい時

['3', '5', '6', '7',,,'N']
list = list(map(int, input().split()))

小数

単一の小数を格納

from decimal import Decimal
A = Decimal(input())

複数の小数を格納

from decimal import Decimal
A, B = map(Decimal, input().split())

floatでいんじゃね?と思いがちだが例えば以下のような問題
C - Multiplication 3
floatなどで計算しても倍精度浮動小数点数の精度の問題から再提出を食らう。

なぜダメかの解説はその道のオタクに任せます。(丸投げ)
浮動小数点数オタクが AtCoder Beginner Contest 169 のC問題をガチで解説してみる

もしDecimalを使用した場合以下のコードとなる。

from decimal import Decimal

A, B = map(Decimal, input().split())
print(int(A*B))

文字

文字列

N = input()

受け取る文字列数が決まっており、リストにしたいとき(Iは回数)

list = [input() for i in range(I)]

例えば3回入力されると決まっており、

orange
grape
strawberry

と入力した場合listの中身は

['orange', 'grape', 'strawberry']

が代入される

色々わかり次第追記していきます。

1
2
1

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
2