슈콩

[BOJ] 백준 1292 쉽게 푸는 문제 본문

Algorithms/Baekjoon

[BOJ] 백준 1292 쉽게 푸는 문제

shukong 2025. 9. 10. 14:05

[문제]

https://www.acmicpc.net/problem/1292

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	int a = Integer.parseInt(st.nextToken());
    	int b = Integer.parseInt(st.nextToken());
    	int result = func(b) - func(a-1);
    	System.out.println(result);
    }
    private static int func(int n) {
    	int result = 0;
    	int i = 1;
    	while(n>i) { 
    		result += i*i;
    		n -= i++;
    	}
    	return (result + n*i);
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 1476 날짜 계산  (0) 2025.09.10
[BOJ] 백준 1456 거의 소수  (0) 2025.09.10
[BOJ] 백준 1256 사전  (0) 2025.09.10
[BOJ] 백준 1193 분수찾기  (0) 2025.09.09
[BOJ] 백준 10610 30  (0) 2025.09.08