# BOJ01292 쉽게 푸는 문제

https://www.acmicpc.net/problem/1292 (opens new window)

동호는 내년에 초등학교를 입학한다. 동호 어머니는 수학 선행 학습을 위해 쉽게 푸는 문제를 동호에게 주었다.

이 문제는 다음과 같다. 1을 한 번, 2를 두 번, 3을 세 번, 이런 식으로

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 ...

이러한 수열을 만들고 어느 일정한 구간을 주면 그 구간의 합을 구하는 것이다.

# 구간합

누적합 (Prefix Sum, Cumulative Sum) (opens new window)

# 제출 코드

아래는 최종 제출 코드이다.

public class BOJ1292 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int[] array = new int[1001];

        calculate(array);

        int a = scanner.nextInt();
        int b = scanner.nextInt();

        System.out.println(array[b] - array[a - 1]);
    }

    private static void calculate(int[] array) {
        int number = 1;
        int remainNumber = number;
        for (int i = 1; i <= 1000; i++) {
            array[i] = number + array[i - 1];
            remainNumber--;

            if (remainNumber == 0) {
                number++;
                remainNumber = number;
            }
        }
    }
}
#BOJ #수학 #구현
last updated: 12/23/2021, 7:03:58 PM