Algorithm/BOJ
#1292 쉽게 푸는 문제
print("스테코더")
2022. 11. 6. 16:16
쉽게 푸는 문제
문제 풀이 전략
💡 수열을 만들 때 구하고자 하는 범위까지의 수열만 생성하면 됨!! (범위가 3~7이면 7개만 생성하면 됨 → 1 2 2 3 3 3 4)
- 범위 (a, b)를 입력 받음
- 수열(arr)을 만듦 → 배열의 인덱스가 1001일 경우 중단
- 1 ~ b까지 1일 때
- →1개의 index에 1 대입, 2일 때 2개의 index에 2 대입 ... b일 때 b개의 index에 b 대입
- (구하고자 하는 범위의 제한 조건이 1000이기 때문)
- 범위 안의 수열의 합을 구함
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class no_1292 {
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[] arr = new int[1001];
int index = 1;
for (int i = 1; i <= b; i++) {
for (int j = 0; j < i; j++) {
if (index == 1001) break;
arr[index++] = i;
}
}
int sum = 0;
for (int i = a; i <= b; i++) {
sum += arr[i];
}
System.out.println(sum);
br.close();
}
}