728x90
문제
n층의 삼각형을 출력하는 프로그램을 작성하여라. Input, Output의 예제를 참고한다.
입력
첫째 줄에 정수n이 주어진다. (0≤n≤100)
출력
다음 예제와 같이 삼각형 모양으로 ‘*’을 출력한다.(공백의 개수와 별의 개수를 정확하게 확인해주시바랍니다.)
예제 입력
3
예제 출력
*
***
*****
예제 입력
6
예제 출력
*
***
*****
*******
*********
***********
#코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception {
// Please Enter Your Code Here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n;
n = Integer.parseInt(st.nextToken());
int totSize = (2*n)-1;
for(int i = 1; i <=totSize; i++) {
if(i%2==0) continue;
String str = "";
// 좌측 공간확보
int temp = (totSize - i)/2;
for (int j=1; j<=temp;j++){
str += " ";
}
// 별찍기
for(int j=1; j <= i; j++) {
str += "*";
}
System.out.println(str);
}
}
}
|
cs |
반응형