본문 바로가기

OJ

[BOJ] 17087 숨바꼭질 6 (JAVA)

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' 카테고리의 다른 글