[정보] 파이썬 gcd에서 리스트(List) 사용하기


[정보] 파이썬 gcd에서 리스트(List) 사용하기

파이썬에서 최대공약수를 구할 때는 math module을 import해서 gcd() 메서드를 사용합니다. gcd() 메서드는 한 개 이상의 인풋을 입력받을 수 있습니다. import math A = [12,4,18] print(math.gcd(A[0])) # 12 print(math.gcd(A[0], A[1])) # 4 print(math.gcd(A[0], A[1], A[2])) # 2 하지만 배열을 input으로 이용하는 건 안 되더라고요 ㅠ 이런 TypeError가 발생해서요. TypeError: 'list' object cannot be interpreted as an integer import math A = [12,4,18] print(math.gcd(A)) # TypeError: 'list' object cannot be interpreted as an integer 그렇다고 방법이 아예 없는 건 아닙니다. 배열 앞에 *를 붙이면 사용이 가능합니다. import math ...


#gcd #import #list #math #module #리스트 #메서드 #최대공약수 #파이썬

원문링크 : [정보] 파이썬 gcd에서 리스트(List) 사용하기