python_algorithm_basic python 백준 solved

[Python/파이썬] 백준 5341번 - Pyramids

Kwangjin Park

Aug 29, 2024 · 1 min read

Follow

문제

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.

풀이

  • 1부터 입력받은 $n$까지의 합을 구하는 문제
  • 0을 입력하면 break하는 무한반복문 while True: 활용

코드

while True:
    n = int(input())
    k = 0
    if n != 0:
        for i in range(n+1):
           k += i
        print(k)
    else:
        break



chat_bubble 0

chat_bubble 댓글남기기

댓글남기기