https://www.acmicpc.net/problem/17087
17087번: 숨바꼭질 6
수빈이는 동생 N명과 숨바꼭질을 하고 있다. 수빈이는 현재 점 S에 있고, 동생은 A1, A2, ..., AN에 있다. 수빈이는 걸어서 이동을 할 수 있다. 수빈이의 위치가 X일때 걷는다면 1초 후에 X+D나 X-D로 이
www.acmicpc.net
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int S = Integer.parseInt(st.nextToken());
int[] dist = new int[N];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < N; i++) {
int cur = Integer.parseInt(st.nextToken());
dist[i] = Math.abs(cur - S);
}
int max = dist[0];
for (int i = 1; i < N; i++) {
max = euclidean(max, dist[i]);
}
System.out.print(max);
}
static int euclidean(int a, int b) {
if (b == 0) return a;
return euclidean(b, a % b);
}
}
'OJ' 카테고리의 다른 글
[BOJ] 1417 국회의원 선거 (JAVA) (0) | 2023.05.27 |
---|---|
[BOJ] 8892 팰린드롬 (JAVA) (0) | 2023.05.26 |
[BOJ] 19637 IF문 좀 대신 써줘 (JAVA) (0) | 2023.05.24 |
[BOJ] 3460 이진수 (JAVA) (0) | 2023.05.23 |
[BOJ] 25314 코딩은 체육과목 입니다 (JAVA) (0) | 2023.05.22 |