본문 바로가기

알고리즘/Java

(JAVA) 백준 알고리즘 3단계 2439번 : 별찍기 - 2

for문

어려웠다...

자연수 5를 찍으면 5에서 i를 뺀만큼 ("") 빈칸으로 채우고 남은부분을 *로 채운다.

 

소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
public class Main{
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        if(a>=1&&a<=100){
           for(int i=1; i<=a; i++){
                for(int j = 0; j<a-i; j++){
                    System.out.print(" ");
                    }
                    for(int k = 0; k<i; k++){
                        System.out.print("*");
                    }
                System.out.print("\n");
            }
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter