본문 바로가기

알고리즘/Java

(JAVA) 백준 알고리즘 5단계 10996번 : 별 찍기 - 21

실습 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
33
34
35
36
37
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 <= 2*a; i++){
                // 홀수
                if(i % 2 == 1){
                    for(int j = 1; j <= a; j++){
                        if(j % 2 == 1){
                            System.out.print("*");
                        }
                        else{
                            System.out.print(" ");
                        }
                    }
                }
                // 짝수
                else{
                    for(int j = 1; j <= a; j++){
                        if(j % 2 == 1){
                            System.out.print(" ");
                        }
                        else{
                            System.out.print("*");
                        }
                    }
                }
                System.out.print("\n");
            }
     
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter