[백준] 5341

당당·2023년 5월 27일
0

백준

목록 보기
132/179

https://www.acmicpc.net/problem/5341

📔문제

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

4
6
0

📺예제 출력 1

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이므로 그대로 출력하면 답이다!

profile
MySQL DBA 신입 지원

0개의 댓글