PS/BOJ
백준 5341 Pyramids / JAVA
얍연구소장
2023. 4. 12. 22:41
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
> 풀이
import java.util.Scanner;
/*
* 백준 5341 Pyramids
* #브론즈5
*/
public class boj_5341 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int result = 0;
Scanner sc = new Scanner(System.in);
while(true) {
int n = sc.nextInt();
if(n == 0) {
System.exit(0);
}
result = solution(n);
System.out.println(result);
}
}
public static int solution(int n) {
return n * (n+1) / 2; // 등차수열의 합
}
}