๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][2903][์ค‘์•™ ์ด๋™ ์•Œ๊ณ ๋ฆฌ์ฆ˜]

yeun.log 2024. 3. 14. 11:03
๋ฐ˜์‘ํ˜•

์—ฌ๊ธฐ์„œ ์ ์˜ ๊ฐฏ์ˆ˜๋Š” ๋‹จ๋ฉดx๋‹จ๋ฉด์ธ ๊ฒŒ ํฌ์ธํŠธ์ž…๋‹ˆ๋‹ค

import java.util.Scanner;

// 2903	์ค‘์•™ ์ด๋™ ์•Œ๊ณ ๋ฆฌ์ฆ˜
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();   // ์ค‘์•™ ์ ์„ ์ฐ๋Š” ๊ณผ์ •์˜ ์ˆ˜
        in.close();

        if (1 <= n && n <= 15) {
            System.out.print(howManyPoint(n));
        }
    }

    static int howManyPoint(int n) {
        int res = 0;
        int cnt = 2;

        for (int i = 0; i < n; i++) {
            res = (int) (cnt + Math.pow(2, i));
            cnt = res;
        }
        
        return res * res;
    }
}
  • ๋‹จ๋ฉดx๋‹จ๋ฉด์ด ๊ฒฐ๊ณผ์ด๋‹ˆ๊นŒ
  • ๋‹จ๋ฉด์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋จผ์ € ๊ตฌํ•ด์„œ
  • howManyPoint ํ•จ์ˆ˜์—์„œ return์‹œ ๋‹จ๋ฉด res * res ๊ณฑํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋‹จ๋ฉด์€  3, 5, 9, 17, 33....์ด๋Ÿฐ์‹์œผ๋กœ ๋Š˜์–ด๋‚˜๋Š”๋ฐ
  • ๋‹จ๋ฉด = ์ด์ „๊ฐ’ + 2์ œ๊ณฑ๊ทผ ์ž…๋‹ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ๊ฐ’์ด ์—†์„ ๋•Œ ์ดˆ๊ธฐ๊ฐ’์ด 2์ด๋ฏ€๋กœ cnt์— ๋จผ์ € 2๋ฅผ ๋‹ด์•„์ค๋‹ˆ๋‹ค.
  • ์ดํ›„ ๊ฐ’์„ ๋‹ด์•„์ฃผ๊ณ ๋ฅผ ์ฃผ์–ด์ง„ ์ˆ˜ n๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•