https://www.acmicpc.net/problem/5341
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
문제
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 <stdio.h>
int main() {
int n;
while (1)
{
scanf("%d", &n);
if (n == 0)
{
break;
}
int sum = n * (n+1) / 2;
printf("%d\n", sum);
}
return 0;
}
등차수열을 이용하면 된다.
'C > solved' 카테고리의 다른 글
| C) 백준 17499번 : 수열과 시프트 쿼리 (0) | 2023.06.03 |
|---|---|
| C) 백준 23234 : The World Responds (0) | 2023.06.03 |
| C) 백준 27327번 : 時間 (Hour) (0) | 2023.06.03 |
| C) 백준 27331번 : 2 桁の整数 (Two-digit Integer) (0) | 2023.06.03 |
| C) 백준 10189번 : Hook (0) | 2023.06.03 |