728x90
5341번: Pyramids
The input will be a sequence of integers, one per line. The end of input will be signaled by the integer 0, and does not represent the base of a pyramid. All integers, other than the last (zero), are positive.
www.acmicpc.net
문제 풀이
입력값으로 n이 주어질 때, 1부터 n까지의 합을 출력하는 문제이다. 0이 입력되면 종료된다.
메모리 제한이 128MB로 4바이트의 크기를 가지는 int형은 128MB = 128 * 1024KB = 128 * 1024 * 1024B = int형 128 * 1024 * 1024 / 4개 = 33554432개 만들 수 있다.
문제 난이도 상 3300만개까지 DP배열을 만들어줄 필요는 없을것 같아서, 1000만개의 배열만 만들어주었다.
정답 코드
C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define ll long long
int dp[10000001];
int main() {
fast;
for (int i = 1; i < 10000001; i++) dp[i] += dp[i - 1] + i;
int n;
while (1) {
cin >> n;
if (n == 0) break;
cout << dp[n] << "\n";
}
}
python
dp = [0] * 10000001
for i in range(1, 10000001) :
dp[i] += dp[i - 1] + i
while True :
n = int(input())
if n == 0 : break
print(dp[n])
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 5358 - Football Team [C++/python] (0) | 2023.03.02 |
---|---|
[BOJ/백준] 5357 - Dedupe [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 5300 - Fill the Rowboats! [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 4589 - Gnome Sequencing [C++/python] (0) | 2023.03.01 |
[BOJ/백준] 4470 - 줄 번호 [C++/python] (0) | 2023.03.01 |