https://www.acmicpc.net/problem/2485
2485번: 가로수
첫째 줄에는 이미 심어져 있는 가로수의 수를 나타내는 하나의 정수 N이 주어진다(3 ≤ N ≤ 100,000). 둘째 줄부터 N개의 줄에는 각 줄마다 심어져 있는 가로수의 위치가 양의 정수로 주어지며, 가
www.acmicpc.net
# 2485 - Python 3
import sys
input = sys.stdin.readline
def gcd(a, b): # 최소공배수 구하는 함수
while b > 0: # 유클리드 호제법 이용
a, b = b, a % b
return a
n = int(input()) # 심어진 나무 총 수
a = int(input()) # 첫 번째 나무 위치
arr = [] # 나무들 사이의 간격이 저장될 list
for i in range(n-1):
x = int(input())
arr.append(x - a)
a = x
g = arr[0]
for i in range(len(arr)):
g = gcd(g, arr[i]) # 모든 나무 간격들의 최소공배수 구하기
result = 0 # 심어야 하는 나무 수
for i in arr:
result += i // g - 1
print(result)'코딩' 카테고리의 다른 글
| [Python] 백준 #4948. 베르트랑 공준 (0) | 2023.03.20 |
|---|---|
| [Python] 백준 #4134. 다음 소수 (0) | 2023.03.20 |
| [Python] 백준 #1735. 분수 합 (0) | 2023.03.20 |
| [Python] 백준 #13241. 최소공배수 (0) | 2023.03.20 |
| [Python] 백준 #1934. 최소공배수 (0) | 2023.03.20 |