백준

백준 2485 - Java

으엉어엉 2024. 9. 2. 21:41
728x90

문제

 

내풀이

 

import java.util.Scanner;

public class StreetTrees {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] gap = new int[N - 1];
        for (int i = 1; i < N; i++) {
            gap[i - 1] = arr[i] - arr[i - 1];
        }

        int gcd = gap[0];
        for (int i = 1; i < gap.length; i++) {
            gcd = findGCD(gcd, gap[i]);
        }

        int tree = 0;
        for (int num : gap) {
            int numOfIntervals = (num / gcd) - 1;
            tree += numOfIntervals;
        }
        System.out.println(tree);
    }

    private static int findGCD(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

 

728x90

'백준' 카테고리의 다른 글

백준 1929 - Java  (0) 2024.09.03
백준 4134 - Java  (0) 2024.09.03
백준 1735 - Java  (0) 2024.09.02
백준 13241 - Java  (0) 2024.09.02
백준 1934 - java  (0) 2024.09.02