• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[백준] 2447번 : 별 찍기 - 10 with java

22 Nov 2018

Reading time ~1 minute

문제: https://www.acmicpc.net/problem/2447

solution 1

import java.util.*;

public class Main {
    private static final Scanner sc = new Scanner(System.in);
    private static char[][] starMap;

    public static void main(String args[]) {
        int num = sc.nextInt();
        starMap = new char[num][num];

        for (int i = 0; i < num; i++) {
            Arrays.fill(starMap[i], ' ');
        }

        solve(0, 0, num);
        for (int i = 0; i < num; i++) {
            System.out.println(starMap[i]);
        }
    }

    public static void solve (int x, int y, int n) {
        if (n == 1){
            starMap[x][y] = '*';
            return;
        }
        int m = n/3;
        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                if (i==1 && j==1){
                    continue;
                }
                solve(x+m*i, y+m*j, m);
            }
        }
    }
}

solution2

3으로 나눴을 때 x와 y 둘다 몫이 1인 것들이 ‘ ‘이다.



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        process(Integer.parseInt(br.readLine()));
    }

    private static void process(int n) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                int x = i, y = j;
                while(x > 0) {
                    if(x % 3 == 1 && y % 3 == 1) {
                        break;
                    }

                    x /= 3;
                    y /= 3;
                }
                sb.append(x == 0 ? '*' : ' ');
//                1,4,7 즉 나머지가 1인 것들만 자기숫자가 나오고 아닌 것들은 0이 된다.
            }
            sb.append('\n');
        }
        System.out.println(sb);
    }
}


algorithm백준java Share Tweet +1