[백준 9613] GCD (최대공약수)

jimin ·2021년 1월 29일
0

algorithm

목록 보기
3/4

최대 공약수 풀이에는 유클리드 호제법을 사용했다.

⭐ 소스코드

import itertools
    
#### 방법 1 #### 

def gcd(a_val, b_val):
     while b_val != 0:
         a_val = a_val % b_val
         a_val, b_val = b_val, a_val
     return a_val
     
     
#### 방법 2) recursive function ####

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)
    

t = int(input())
for i in range(t):
    sum = 0
    num_list = list(map(int, input().split()))
    perm = list(itertools.combinations(num_list[1:], 2))
    for i in perm:
        sum += gcd(i[0],i[1])
    print(sum)
profile
꿈꾸는 중입니다 :)

0개의 댓글