자기개발👨‍💻/코딩 알고리즘

[python, C++] 백준 1292 쉽게 푸는 문제

천숭이 2022. 2. 3. 20:46

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

 

1292번: 쉽게 푸는 문제

첫째 줄에 구간의 시작과 끝을 나타내는 정수 A, B(1 ≤ A ≤ B ≤ 1,000)가 주어진다. 즉, 수열에서 A번째 숫자부터 B번째 숫자까지 합을 구하면 된다.

www.acmicpc.net

 

- Python

a, b = map(int, input().split())
numbers = []
Sum = 0
for i in range(1,1001):
    for j in range(i) :
        numbers.append(i)

print(sum(numbers[a-1:b]))

 

- C++

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <charconv>
using namespace std;

int main() {
	int a, b, Sum = 0;
	vector<int> v;
	cin >> a >> b;
	for (int i = 1; i <= 1001; i++) {
		for (int j = 1; j <= i; j++) {
			v.push_back(i);
		}
	}

	for (int i = a; i <= b; i++) {
		Sum += v[i-1];
	}
	cout << Sum;
}