백준 9770번: GCD #Python

ColorlessDia·2024년 10월 25일

algorithm/baekjoon

목록 보기
341/808
import sys

def GCD(x, y):
    
    if y == 0:
        return x

    return GCD(y, x % y)

number_list = []

while True:
    line = sys.stdin.readline().rstrip()

    if line == '':
        break
    
    number_list += list(map(int, line.split()))

sorted_number_list = sorted(number_list)

max_GCD = 0

for i in range(len(sorted_number_list)):
    
    for j in range(i + 1, len(sorted_number_list)):
        x, y = sorted_number_list[j], sorted_number_list[i]

        gcd = GCD(x, y)

        if max_GCD < gcd:
            max_GCD = gcd
    
print(max_GCD)

0개의 댓글