I don't know how many brews anymore, but it is a summary of the standard input set for Atcoder.
I'm sloppy with Atcoder, but I haven't been devoted at all, so I can't remember the standard input forever. So, I would like to organize the standard input once and systematically organize the faster standard input.
This article summarizes the minimum standard input for solving the basic ABC-ABC problem in the basics. Standard input using sys and numpy will be covered in another article.
The pointed out part has been corrected (2020/05/06).
input
N (string or number)
code
sample.py
#When receiving in str type
s = input()
#When receiving as an int type
s = int(input())
#float type(Decimal)When receiving at
s = float(input())
As a caveat, note that input () is all str type, even if it is a number instead of a string.
input
A B
Input example
Alice Bob Charlie
** Code example 1 **
sample.py
#When receiving as list type
s = input().split()
#output
print(s)
>>['Alice', 'Bob', 'Charlie']
print(s[0])
>>Alice
print(s[0][0])
>>A
** Code example 2 **
sample.py
#When receiving as a string
A, B, C = input().split()
#output
print(A)
>>Alice
print(A,B,C)
>>Alice Bob Charlie
Input example
1 3
** Code example 1 **
sample.py
A, B = map(int, input().split())
#output
print(A)
>>1
print(A,B)
>>1 3
input
A1 A2 A3...AN
Input example
1 3 4 5 6
** Code example 1 **
sample.py
#Get as list type
l = list(map(int, input().split()))
#output
print(l)
>>[1, 3, 4, 5, 6]
input
N M A1 A2 : AN
Input example
3 4 2 3 3 1
** Code example 1 (int type) ** Convert (N, 1) matrix to (1, N) matrix by generating an empty list and storing it in the list in order from the top
sample.py
N, M = map(int, input().split())
#Empty list
A = []
#List A append()Store using
for _ in range(M):
A.append(int(input())
#output
print(A)
>>[2, 3, 3, 1]
** Code example 2 (list comprehension) **
sample.py
N, M = map(int, input().split())
#List comprehension
A = [int(input()) for _ in range(M)]
#output
print(A)
>>[2, 3, 3, 1]
input
N x1 x2 x3 .. xN y1 y2 y3 .. yN
Input example
3 1 2 3 4 5 6
** Code example **
sample.py
N = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
#output
print(x)
>>[1, 2, 3]
input
N x1 y1 x2 y2 : xN yN
Input example
5 1 2 3 4 5 6 7 8 9 10
** Code example 1 (store x and y independently) **
sample.py
N = int(input())
xy = [map(int, input().split()) for _ in range(N)]
x, y = [list(i) for i in zip(*xy)]
#output
print(x)
>>[1, 3, 5, 7, 9]
print(x[1]+y[1])
>>7
** Code example 2 (stored as [x i </ sub>, y i </ sub>]) **
sample.py
N = int(input())
l = [list(map(int, input().split())) for l in range(N)]
#output
print(l)
>>[[1, 2], [3, 4], [5, 6]]
Input example
5 1 a 3 b 5 c 7 d 9 e
sample.py
N = int(input())
list = []
for i in range(N):
a,b=input().split()
list.append([int(a), b])
#output
print(list)
>>[[1, 'a'], [3, 'b'], [5, 'c'], [7, 'd'], [9, 'e']]
print(type(list[0][0]))
>><class'int'>
print(type(list[0][1]))
>><class'str'>
It was very easy to understand, so I quoted it from this article. Standard input of python3 in competition professionals
input
N t1 x1 y1 t2 x2 y2 : tN xN yN
Input example
2 3 1 2 6 1 1
sample.py
#First, create a list with the length of the input variable. * N=In case of 5,t = [0,0,0,0,0]
N = int(input())
t = [0] * N
x = [0] * N
y = [0] * N
for i in range(N):
#Substitute in order from the top
t[i], x[i], y[i] = map(int, input().split())
#output
print(t)
>>[3, 6]
I quoted from this article Tips you should know when programming in Python3 (input)
input
X Y A B C P1 P2 P3...PA Q1 Q2 Q3...QB R1 R2 R3...RC
Problem: E --Red and Green Apples
Input example
1 2 2 2 1 2 4 5 1 3
sample.py
X, Y, A, B, C = map(int, input().split())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]
#output
print(P)
>>[2, 4]
#There is no problem with this form
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
input
N M A1,1 A1,2...A1 ,N : AM,1 AM,2...AM,N
** Input example (3,3) Matrix data **
3 3 2 1 3 1 3 3 2 2 1
** Code example 1 (int type) **
sample.py
N, M = map(int,input().split())
#List comprehension
#Read the list in order from the top and store it in the list.
a = [list(map(int, input().split())) for l in range(M)]
#output
print(a)
>>[[2, 1, 3], [1, 3, 3], [2, 2, 1]]
print(a[0])
>>[2, 1, 3]
print(a[0][0])
>>2
print(type(a[0][0]))
>><class'int'>
A little introduction to ** numpy **. You can use ** numpy ** to convert a list to a ** numpy array ndarray **. By performing this conversion, not only matrix operations can be performed, but also execution speed can be increased and code can be simplified.
sample.py
import numpy as np
#Appropriate list
list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Convert to ndarray
list_ndarray = np.array(list)
print(list_ndarray)
>>[[1 2 3]
[4 5 6]
[7 8 9]]
#Array size(Element count)
print(list_ndarray.size)
>>9
#Array shape
print(list_ndarray.shape)
>>(3, 3)
#Mold
print(type(list_ndarray))
>><class'numpy.ndarray'>
#numpy array → list
list_list = list_ndarray.tolist()
print(list_list)
>>[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(type(list_list))
>><class'list'>
-[Standard input in python3] (https://qiita.com/pyu666/items/6b8cfefc1ea994639683) ・ Tips you should know when programming in Python3 (input) -Standard input of python3 in competition pros, etc. ・ Python de Competitive Programming
See the article below for details. ・ [What are standard input and standard output? ] (https://qiita.com/angel_p_57/items/03582181e9f7a69f8168)
Make corrections as appropriate.
Continue to another article.
Recommended Posts