치춘짱베리굿나이스

[백준] 5341 본문

C C++/알고리즘풀이

[백준] 5341

치춘 2023. 7. 5. 12:42

Pyramids

문제

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.

풀이

#include <iostream>

int main(void) {
    int temp, sum;
    while (true) {
        std::cin >> temp;
        if (temp == 0) break;
        sum = 0;
        while (temp) {
            sum += temp--;
        }
        std::cout << sum << "\n";
    }
}

'C C++ > 알고리즘풀이' 카테고리의 다른 글

[백준] 2193  (0) 2023.07.06
[백준] 1912  (0) 2023.07.06
[백준] 2217  (0) 2023.07.04
[백준] 11501  (0) 2023.07.04
[백준] 12018  (0) 2023.07.03
Comments