본문 바로가기
Study (etc)/Problem Solving

[BOJ / C++] 5341번 : Pyramids

by Haren 2022. 12. 17.

문제

A pyramid of blocks is constructed by first building a base layer of n blocks and then adding n-1 blocks to the next layer. This process is repeated until the top layer only has one block.

You must calculate the number of blocks needed to construct a pyramid given the size of the base. For example, a pyramid that has a base of size 4 will need a total of 10 blocks.

입력

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.

출력

For each positive integer print the total number of blocks needed to build the pyramid with the specified base.

Solved.ac 레벨

브론즈 V

풀이

피라미드의 기반석의 개수가 주어졌을 때 피라미드를 완성하는 데에 필요한 벽돌의 총 개수를 출력하는 문제다.

기반석이 4개라고 가정하면 1층에 4개, 2층에 3개, 3층에 2개, 4층에 1개가 필요하여 총 10개가 필요하다.

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    while(1){
        int input = 0;
        int cnt = 0;

        cin >> input;
        
        if(input == 0) return 0;
        else{
            for(int i = input; i > 0; i--){
                cnt += i;
            }
            cout << cnt << '\n';
            input = 0;
        }
    }
    return 0;
}

 

 

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

 

'Study (etc) > Problem Solving' 카테고리의 다른 글

[BOJ / C++] 26545번 : Mathematics  (0) 2022.12.20
[BOJ / C++] 26574번 : Copier  (0) 2022.12.20
[BOJ / C++] 20254번 : Site Score  (0) 2022.12.15
[BOJ / Python3] 8437번 : Julka  (0) 2022.12.13
[BOJ / C++] 18301번 : Rats  (0) 2022.12.12