코드 1 (itertools, math 모듈 사용)
from itertools import combinations
from math import gcd
tc = int(input())
for _ in range(tc):
list = [*map(int,input().split())]
res = 0
for i in combinations(list,2):
res = max(res, gcd(*i))
print(res)
코드 2 (모듈 사용 X)
def gcd(a,b): # 유클리드 호제법
while b:
a,b = b, a%b
return a
tc = int(input())
for _ in range(tc):
list = [*map(int,input().split())]
res = 0
for i in range(len(list)):
for j in range(i+1,len(list)):
res = max(res, gcd(list[j],list[i]))
print(res)
'백준' 카테고리의 다른 글
[백준] 1504번 특정한 최단 경로 파이썬 코드 (0) | 2023.12.16 |
---|---|
[백준] 2749번 피보나치 수 3 파이썬 코드 (0) | 2023.12.14 |
[백준] 9471번 피사노 주기 파이썬 코드 (0) | 2023.12.14 |
[백준] 10826번 피보나치 수 4 파이썬 코드 (0) | 2023.12.13 |
[백준] 1463번 1로 만들기 파이썬 코드 (0) | 2023.12.13 |
코드 1 (itertools, math 모듈 사용)
from itertools import combinations
from math import gcd
tc = int(input())
for _ in range(tc):
list = [*map(int,input().split())]
res = 0
for i in combinations(list,2):
res = max(res, gcd(*i))
print(res)
코드 2 (모듈 사용 X)
def gcd(a,b): # 유클리드 호제법
while b:
a,b = b, a%b
return a
tc = int(input())
for _ in range(tc):
list = [*map(int,input().split())]
res = 0
for i in range(len(list)):
for j in range(i+1,len(list)):
res = max(res, gcd(list[j],list[i]))
print(res)
'백준' 카테고리의 다른 글
[백준] 1504번 특정한 최단 경로 파이썬 코드 (0) | 2023.12.16 |
---|---|
[백준] 2749번 피보나치 수 3 파이썬 코드 (0) | 2023.12.14 |
[백준] 9471번 피사노 주기 파이썬 코드 (0) | 2023.12.14 |
[백준] 10826번 피보나치 수 4 파이썬 코드 (0) | 2023.12.13 |
[백준] 1463번 1로 만들기 파이썬 코드 (0) | 2023.12.13 |