[Bronze V] Pyramids - 5341

문제 링크

성능 요약

메모리: 1116 KB, 시간: 0 ms

분류

수학, 구현, 사칙연산

제출 일자

2024년 11월 7일 14:46:03

문제 설명

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>
 
#include<string.h>
 
#include<stdlib.h>
 
void total(int base)
 
{
 
	int ans = 0;	for(int a = base; a > 0; a--)
 
	{
 
		ans += a;
 
		}	
 
	printf("%d", ans);
 
	}
 
int main(int argc, char *argv[])
 
{
 
	int in = 0;
 
	int try = 0;
 
	while(1)
 
	{
 
		scanf("%d", &in);
 
		try++;
 
		if (in == 0) break;
 
		
 
		if(try > 1)
 
			printf("\n");
 
		
 
		total(in);
 
		}
 
	
 
		
 
	return 0;	
 
}