Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Matrix
- database
- implement
- Binary Search
- Binary Tree
- 파이썬
- Math
- Number Theory
- Counting
- simulation
- sorting
- two pointers
- Stack
- hash table
- geometry
- string
- Class
- Method
- array
- bit manipulation
- Data Structure
- 구현
- SQL
- 자바
- Tree
- dynamic programming
- java
- 코딩테스트
- 코테
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[백준 온라인 저지] 2485. 가로수 본문
1. 입력
- 첫째 줄에는 이미 심어져 있는 가로수의 수를 나타내는 하나의 정수 N이 주어진다(3 ≤ N ≤ 100,000).
- 둘째 줄부터 N개의 줄에는 각 줄마다 심어져 있는 가로수의 위치가 양의 정수로 주어지며, 가로수의 위치를 나타내는 정수는 1,000,000,000 이하이다. 가로수의 위치를 나타내는 정수는 모두 다르다.
2. 출력
- 모든 가로수가 같은 간격이 되도록 새로 심어야 하는 가로수의 최소수를 첫 번째 줄에 출력한다.
3. 코드
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
int a = Integer.valueOf(br.readLine());
int[] num = new int[n-1];
for(int i=0 ; i<n-1 ; i++){
int b = Integer.valueOf(br.readLine());
num[i] = b-a;
a = b;
}
int gcd = num[0];
for(int i=1 ; i<n-1 ; i++)
gcd = findGCD(Math.max(gcd,num[i]), Math.min(gcd,num[i]));
int answer = 0;
for(int i=0 ; i<n-1 ; i++)
answer += num[i]/gcd -1;
System.out.print(answer);
}
static int findGCD(int a, int b){
if(b==0) return a;
return findGCD(b, a%b);
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 4948. 베르트랑 공준 (0) | 2023.04.20 |
---|---|
[백준 온라인 저지] 1929. 소수 구하기 (0) | 2023.04.20 |
[백준 온라인 저지] 1735. 분수 합 (0) | 2023.04.19 |
[백준 온라인 저지] 13241. 최소공배수 (0) | 2023.04.18 |
[백준 온라인 저지] 1934. 최소공배수 (0) | 2023.04.18 |