백준 2444번 www.acmicpc.net/problem/2444

 

 

 

풀이

 

중첩 for문을 이용해서 풀이했다. 별찍기 문제 원리를 파악해둘것!

 
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
import java.util.*;
 
public class for_2444 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        
        for (int i=1; i<=n; i++) {
            for (int j=0; j<n-i; j++) {
                System.out.print(" ");
            }
            for (int j=0; j<2*i-1; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
        
        for (int i=1; i<n; i++) {
            for (int j=0; j<i; j++) {
                System.out.print(" ");
            }
            for (int j=0; j<2*n-2*i-1; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
 
}
cs
댓글