[Algorithm study] 백준 1292번

SeokHwan An·2022년 1월 21일
0

문제 출처 : https://www.acmicpc.net/problem/1292

문제 접근

문제에서 주어지는 A,B의 범위를 파악하면 (1 <= A <= B <= 1,000) 이므로 1001칸의 정수형 배열을 만들어 1,2,2,3,3,3..의 수열을 배열 내에 차례대로 입력받았습니다.

소스 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine());
        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int[] num = new int[1001];
        int check = 1;
        int cnt = 1;
        for(int i = 1; i < num.length; i++){
            num[i] = check;
            cnt--;
            if(cnt == 0) {
                check++;
                cnt = check;
            }
        }
        int answer = 0;
        for(int i = A; i <= B; i++){
            answer += num[i];
        }
        System.out.println(answer);
    }
}

0개의 댓글