• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

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

[백준] 2448번 : 별 찍기 - 11 with java8

23 Nov 2018

Reading time ~2 minutes

후 이제 이 포스팅 이후로는 java는 (최대한) java8로 풀 예정이다.

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

solution 1

cpp로는 되는 로직인데.. 왜 java로 하면 시간초과 되는지 모르겠다. 그래서 되는 로직은 밑에 썼다.

참고영상: https://www.youtube.com/watch?v=WjmEVp-Lgns&feature=youtu.be

package com.company;

import java.util.Scanner;

//가장 작은 삼각형 중에서 가장 위의 뾰족한 별을 중심으로 그리는 거다.
public class Main {

    private static char[][] starMap = new char[3072][6144];
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args){
        int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 2*n-1; j++) {
                starMap[i][j] = ' ';
            }
        }

        makeStar(n, n-1, 0);
        // 출력방법 : 문자 반복 출력
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 2*n-1; j++) {
                System.out.print(starMap[i][j]);

            }
            System.out.println('\n');
        }

    }

    public static void makeStar(int n, int x, int y){
        if (n == 3){ // 별을 그린다
            starMap[y][x] = '*';
            starMap[y + 1][x - 1] = '*';
            starMap[y + 1][x + 1] = '*';
            starMap[y + 2][x - 2] = '*';
            starMap[y + 2][x - 1] = '*';
            starMap[y + 2][x] = '*';
            starMap[y + 2][x + 1] = '*';
            starMap[y + 2][x + 2] = '*';
            return;
        }
        makeStar(n / 2, x, y);
        makeStar(n / 2, x - (n / 2), y + (n / 2));
        makeStar(n / 2, x + (n / 2), y + (n / 2));
    }
}

solution 2

import java.util.*;
public class Main {
    static String[] starMap;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();

        String starMap[] = new String[n];
        starMap[0] = "  *  ";
        starMap[1] = " * * ";
        starMap[2] = "*****";

        for (int i = 1; 3 * (int)Math.pow(2, i) <= n; ++i) {
            int bottom = 3 * (int)Math.pow(2, i);
            int middle = bottom / 2;

            for (int j = middle; j < bottom; ++j) {
                starMap[j] = starMap[j - middle] + " " + starMap[j - middle];
                // 전의 것을 가져와서 두배로 만들어서 낸다.
            }

            // 적합한 위치로 보내는 코드
            String space = String.join("", Collections.nCopies(middle," "));            
            for (int j = 0; j < middle; ++j) {
                starMap[j] = space + starMap[j] + space;
            }
        }

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


}


algorithm백준javajava8 Share Tweet +1