본문 바로가기
BaekJoon/Bronze

[BOJ/JAVA] 백준 5341 : Pyramids (자바)

by HoonSikE 2023. 7. 15.
반응형
SMALL
문제 정보
  문제명   - Pyramids
  난이도   - 브론즈 V
문제 번호 - 5341번

문제 링크

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.

예제 입력/출력
예제 입력 예제 출력
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();
    }
}

 


BaekJoon List
 

BaekJoon List

BOJ Start!! ● [BOJ] 백준 회원가입, 시작하는 법 ● [BOJ] 등급(티어) 및 Solved.AC 적용 ● [BOJ/JAVA] 백준 소스코드 제출 시 유의사항(자바) Bronze ● Bronze V  - ● Bronze IV  - ● Bronze III  -..

han-hoon.tistory.com


  

기회는 준비된 자에게 찾아온다.

 


 

반응형
LIST

댓글