๊ธฐ๋ก๋ฐฉ

BOJ_2448 : ๋ณ„ ์ฐ๊ธฐ - 11 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2448 : ๋ณ„ ์ฐ๊ธฐ - 11

Soom_1n 2023. 8. 28. 23:16

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2448๋ฒˆ: ๋ณ„ ์ฐ๊ธฐ - 11

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ ํ•ญ์ƒ 3×2k ์ˆ˜์ด๋‹ค. (3, 6, 12, 24, 48, ...) (0 ≤ k ≤ 10, k๋Š” ์ •์ˆ˜)

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N์ด 3x2^k (0<= k <=10)๋กœ ์ž…๋ ฅ๋œ๋‹ค.
    • ์ถœ๋ ฅ ํ˜•์‹์ฒ˜๋Ÿผ ์žฌ๊ท€ ํ˜•์‹์˜ ํ”ผ๋ผ๋ฏธ๋“œ ๋ณ„์ฐ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ๋ˆ„๊ฐ€๋ด๋„ ์žฌ๊ท€์ ์ธ ํŠน์ง•์ด ๋ณด์ด๋Š”๋ฐ, ๊ทœ์น™์„ ์ •์˜ํ•ด๋ณด์ž.
  *  
 * * 
*****
  • ๋จผ์ € N์ด ๊ฐ€์žฅ ์ž‘๊ฒŒ ์ž…๋ ฅ๋์„ ๊ฒฝ์šฐ๋Š” 3์ผ ๋•Œ์ด๋‹ค.
  • ๋” ์ž‘์€ ๋‹จ๊ณ„๋กœ ๊ฐˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์ค„ ๋ฒˆํ˜ธ๊ฐ€ 1, 2, 3 ์ผ ๋•Œ๋กœ ๋‚˜๋ˆ„์–ด์„œ *, * *, ***** ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค.
     *     
    * *    
   *****   
  *     *  
 * *   * * 
***** *****
  • ๋‹ค์Œ์€ N์ด 6์ผ๋•Œ ์ด๋‹ค. ์•ž์„  N์ด 3์ธ ๊ฒฝ์šฐ์˜ ๋ชจ์–‘์ด 3๋ฒˆ๋“ค์–ด๊ฐ„๋‹ค.
  • ์—ฌ๊ธฐ์„œ ๊ทœ์น™์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ค‘๊ฐ„ ์•„๋ž˜์ชฝ์œผ๋กœ๋Š” N์ด 3์ธ ํ”ผ๋ผ๋ฏธ๋“œ๊ฐ€ 2๊ฐœ, ๋’ค์ง‘ํžŒ ๊ณต๋ฐฑ ์‚ผ๊ฐํ˜•์ด ๊ทธ ์‚ฌ์ด์— ๋“ค์–ด๊ฐ„ ๋ชจ์–‘์ด๋‹ค.
           *           
          * *          
         *****         
        *     *        
       * *   * *       
      ***** *****      
     *           *     
    * *         * *    
   *****       *****   
  *     *     *     *  
 * *   * *   * *   * * 
***** ***** ***** *****
  • N์ด 12์ธ ๊ฒฝ์šฐ๋Š” N์ด 6์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 2๋ฒˆ๋‚˜์˜ค๊ณ  ๊ฐ€์šด๋ฐ ๋’ค์ง‘ํžŒ ๊ณต๋ฐฑ ์‚ผ๊ฐํ˜•์ด ๋“ค์–ด๊ฐ„ ๋ชจ์Šต์ด๋‹ค.
    • ํฌ์ŠคํŒ… ํ•˜๋‹จ์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ํŽธํ•  ๊ฒƒ์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ ๊ฐ ์ค„๋งˆ๋‹ค ์ง์ „ ์‚ผ๊ฐํ˜•์˜ ๋†’์ด๋ฅผ ์•Œ์•„์•ผํ•˜๋Š”๋ฐ, ์–ด๋Š 3*2^k ~ 3*2^k' ๋ฒ”์œ„์— ์†ํ•œ์ง€ ์ฐพ๊ณ , 3*2^k๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.
  • ์ง์ „ ์‚ผ๊ฐํ˜•์˜ ๋†’์ด(3*2^k)๋ฅผ ์ฐพ์•˜์œผ๋ฉด, ํ˜„์žฌ ์ค„ ๋ฒˆํ˜ธ์—์„œ ์ง์ „ ๋†’์ด๋ฅผ ๋นผ์„œ ์žฌ๊ท€๋ฅผ ์œ„ํ•œ ์ค„ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ๊ตฌํ•œ ์ค„ ๋ฒˆํ˜ธ๊ฐ€ ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์‚ผ๊ฐํ˜•์˜ ์ค„ ๋ฒˆํ˜ธ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
    private static StringBuilder sb;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        sb = new StringBuilder();

        int N = Integer.parseInt(br.readLine());

        for (int i = 1; i <= N; i++) {
            for (int j = 0; j < N - i; j++) {
                sb.append(' ');
            }
            recursion(i);
            for (int j = 0; j < N - i; j++) {
                sb.append(' ');
            }
            sb.append('\n');
        }
        sb.deleteCharAt(sb.length() - 1);
        bw.write(sb.toString());
        bw.flush();
    }

    private static void recursion(int n) {
        if (n == 1) {
            sb.append('*');
        } else if (n == 2) {
            sb.append('*');
            sb.append(' ');
            sb.append('*');
        } else if (n == 3) {
            for (int i = 0; i < 5; i++) {
                sb.append('*');
            }
        } else {    // ์žฌ๊ท€
            int before_max = find_before_max(n);
            recursion(n - before_max);
            for (int i = 0; i < (before_max * 2 - n + 1) * 2 - 1; i++) {
                sb.append(' ');
            }
            recursion(n - before_max);
        }
    }

    private static int find_before_max(int n) {
        if (n <= 3) {
            return 0;
        }

        int i = 0;
        double num = 3 * Math.pow(2, i);

        while (num < n) {
            num = 3 * Math.pow(2, ++i);
        }

        return (int) (3 * Math.pow(2, i - 1));
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • main ๋ฉ”์„œ๋“œ์—์„œ ํ”ผ๋ผ๋ฏธ๋“œ ์•ž ๋’ค ๊ณต๋ฐฑ์„ ์ฑ„์šด๋‹ค.
  • ์žฌ๊ท€ ๋ฉ”์„œ๋“œ recursion์—์„œ ํ”ผ๋ผ๋ฏธ๋“œ ์•ˆ์ชฝ์˜ ๋ณ„๊ณผ ๊ณต๋ฐฑ์„ ์ฑ„์šด๋‹ค.
  • find_before_max ๋ฉ”์„œ๋“œ์—์„œ ์ง์ „ ์‚ผ๊ฐํ˜•์˜ ๋†’์ด๋ฅผ ๊ตฌํ•œ๋‹ค.
  • ์žฌ๊ท€ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋œ๋‹ค.
    • ์ค„๋ฒˆํ˜ธ n์ด 1, 2, 3์ธ ๊ฒฝ์šฐ๋Š” ์ •ํ•ด์ง„ ๋ชจ์–‘์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๊ทธ ์™ธ์—๋Š” ์ง์ „ ์‚ผ๊ฐํ˜•, ๊ณต๋ฐฑ ์‚ผ๊ฐํ˜•, ์ง์ „ ์‚ผ๊ฐํ˜•์„ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.
    • ์ง์ „ ์‚ผ๊ฐํ˜•์„ ์ถœ๋ ฅํ•  ๋•Œ ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์˜ค๋žœ๋งŒ์— ์žฌ๊ท€ ๋ฌธ์ œ์— ์ž˜๋ชป๊ฑธ๋ ค์„œ ์•„์ฃผ์•„์ฃผ ๊ณ ์ƒํ–ˆ๋‹ค.
    • ๊ณ„์† ํ’€๋ฆฌ์ง€ ์•Š์•˜์ง€๋งŒ, ์˜ค๊ธฐ๋กœ ํ’€์ด๋ฅผ ๋ณด์ง€์•Š๊ณ  ์ง„ํ–‰ํ•ด์„œ 3์ผ์ด๋‚˜ ๊ฑธ๋ ธ๋‹ค.
    • ์ฝ”ํ…Œ ์ŠคํŠธ๋ฆญ์ด ๋Š๊ฒผ๋‹ค๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๋งˆ์Œ๊ฐ€์ง์œผ๋กœ ํ’€์ด๋ฅผ ์ตœ๋Œ€ํ•œ ๋ณด์ง€ ์•Š์•˜๋‹ค.
  • ์•„์‰ฌ์šด ์ ์€ ์กฐ๊ธˆ ๋ฒˆ๊ฑฐ๋กœ์›Œ์„œ ๋ฌธ์ œ๋ฅผ ์†์œผ๋กœ ํ’€์ดํ•ด๊ฐ€๋ฉฐ ์ ‘๊ทผํ•˜์ง€ ์•Š๋‹ค๊ฐ€, ๊ฒฐ๊ตญ ์˜ค๋Š˜ ์†์œผ๋กœ ์ ์–ด๊ฐ€๋ฉฐ ํ’€์—ˆ๋”๋‹ˆ ํ’€๋ ธ๋‹ค.
    • ์ง„์ž‘์— ๊ผผ๊ผผํ•˜๊ฒŒ ํ’€์ดํ•  ๊ป„ ํ›„ํšŒ๋œ๋‹ค.
    • ๋‹ค์Œ์€ ํ’€์ดํ•˜๋ฉด์„œ ๊ทธ๋ฆฐ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ฐธ๊ณ  ๊ทธ๋ฆผ์ด๋‹ค.

728x90