본문 바로가기

Coding Test

백준 1000번 - split, map함수

문제 : 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하라.

입력 : 첫째 줄에 A와 B가 주어진다.(0<A,B<10)

출력 : 첫째 줄에 A+B를 출력한다. 

 

split 함수 이용 : 입력값을 두 개 이상으로 구분 (기본:띄어쓰기)

[답안1]
A, B = input().split()  # int(input().split()):error - int함수는 리스트를 정수형으로 바꿀 수 없다.
x = int(A)
y = int(B)
print(x+y)

map 함수 이용 : map(함수, 반복가능한 자료형: list, tuple)

[답안2]
A, B = map(int, input().split()) # map이 int를 리스트 각각의 값에 적용시킴
print(A+B)

 

 

출처 :

https://ccamppak.tistory.com/entry/mapint-inputsplit%EC%97%90-%EB%8C%80%ED%95%B4?category=1026074 

 

[파이썬 / Python] map(int, input().split())에 대해

제목의 식은 백준의 다른 문제를 풀이할 때 계속해서 사용하게 될 것이다. 따라서 좀 더 구체적으로 map(int, input().split()) 을 구성하는 함수들이 무엇이며 어떻게 변형할 수 있는지 알려드리고자

ccamppak.tistory.com

 

'Coding Test' 카테고리의 다른 글

백준 1110번 - while문  (0) 2022.02.19
백준 10951번 - while문 탈출  (0) 2022.02.18
백준 8393, 2741, 2742 - for문  (0) 2022.02.17
백준 15552번 - sys.stdin.readline()  (0) 2022.02.13
백준 2588번 - %, range, list  (0) 2022.02.08