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.
예제 입력/출력
예제 입력
예제 출력
4
6
0
10
21
알고리즘 분류
● 수학 ● 구현 ● 사칙연산
소스코드
package Lv1_Bronze;
import java.io.*;
/**
* @author HanHoon
* @category 수학, 구현, 사칙연산
* https://www.acmicpc.net/problem/5341
*/
public class BOJ_B5_5341_Pyramids {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder str = new StringBuilder();
while(true){
int N = Integer.parseInt(br.readLine());
if(N == 0)
break;
int result = 0;
for(int n = N; n > 0; n--)
result += n;
str.append(result).append("\n");
}
System.out.print(str);
br.close();
}
}
댓글