백준 2903

hong030·2023년 1월 30일
0
  • solved.ac 기준 브론즈 3단계 문제

풀이)
규칙을 알아내면 쉽게 풀 수 있는 문제이다.
1번 반복할 때 정사각형 한 변에 있는 점의 개수는 2+1
2번 반복할 때 정사각형 한 변에 있는 점의 개수는 4+1
n번째 반복할 때엔 정사각형 한 변에 있는 점의 개수는 2^n + 1이다.
따라서 총 점의 개수는 (2^n + 1) ^ 2이다.

제곱을 계산하는 코드는

int a = (int) Math.pow(2, n) // 2의 n승

이며, pow() 메소드는 double 형을 출력하기 때문에 int로 형변환을 해주어야 한다.

내 코드)

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

public class Main {
	public static void main(String[]args) throws IOException {
	
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(bf.readLine());
		int count = (int)Math.pow(2, n);
		int result = (count+1) * (count+1);
		System.out.println(result);
	}
}
profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글