반응형
https://www.acmicpc.net/problem/17087
문제
처음에 이게 무슨 문제지? 했는데,
3 3 => 동생 3명, 수빈이가 있는 위치
1 7 11 => 동생 3명이 각각 1, 7, 11 위치에 있는 것.
그러면 수빈이가 있는 위치와 동생들끼리의 위치를 각각 새로운 list에 저장해주고,
그 list의 각각의 GCD(최대공배수) 값을 구해서 그 값을 출력해주면 된다.
해결 코드
import sys
input = sys.stdin.readline
def GCD(a, b):
if(b==0):
return a
return GCD(b, a%b)
num, current_point = map(int, input().split())
list_location = list(map(int, input().split()))
span = [abs(current_point-list_location[0])]
for i in range(num-1):
span.append(abs(list_location[i]-list_location[i+1]))
gcd = span[0]
for i in range(1, num):
gcd = GCD(gcd, span[i])
print(gcd)
반응형