반응형
문제
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
풀이
숫자가 주어지면, 몇개의 사각형이 필요하는지를 구하는 문제입니다.
위는 4를 입력했을 때의 피라미드 모양입니다. 총 10개로, 1+2+3+4임을 알 수 있습니다.
for문을 이용하면 쉽게 풀 수 있습니다.
#include <iostream>
using namespace std;
int main() {
while (1) {
int N;
cin >> N;
if (N == 0) break;
int result = 0;
for (int i = 1; i <= N; i++) {
result += i;
}
cout << result << endl;
}
return 0;
}
반응형
'📊 알고리즘' 카테고리의 다른 글
[백준] 1789 - 수들의 합 (0) | 2022.12.29 |
---|---|
[백준] 26545 - A+B (0) | 2022.12.28 |
[백준] 14337 - Helicopter (0) | 2022.12.26 |
[백준] 26489 - Gum Gum for Jay Jay (0) | 2022.12.25 |
[백준] 6840 - Who is in the middle? (0) | 2022.12.24 |