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
High School > PLU High School Programming Contest > PLU 2013 > Novice 5번
import java.util.Scanner;
public class Code5341 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
if(n==0){
break;
}
int answer=n*(n+1)/2;
System.out.println(answer);
}
}
}
1
부터 n
까지의 합
을 구하는 문제이다.
공식은 n(n+1)/2
이므로 그대로 출력하면 답이다!