๋ฐ์ํ
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๋งํผ ๋ฐ๋ณตํฉ๋๋ค.
๋ฐ์ํ
'๐ธ Algorithm > ๐ธ ๋ฐฑ์ค BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค][java][1193][๋ถ์์ฐพ๊ธฐ] (0) | 2024.03.21 |
---|---|
[๋ฐฑ์ค][java][2292][๋ฒ์ง] (0) | 2024.03.15 |
[๋ฐฑ์ค][java][2720][์ธํ์ ์ฌ์ฅ ๋ํ] (0) | 2024.03.14 |
[๋ฐฑ์ค][java][11005][์ง๋ฒ ๋ณํ 2] (0) | 2024.03.13 |
[๋ฐฑ์ค][java][2745][์ง๋ฒ ๋ณํ] (3) | 2024.03.12 |