BJ1074 Z

·2022년 4월 17일
0

백준 알고리즘

목록 보기
11/34

https://www.acmicpc.net/problem/1074

문제가 어려워보이기도 하지만, 재귀함수를 이용해 구현하면 생각보다 간단한 코드로 해결할 수 있다.

R행 C열을 입력 받게 되면 가장 큰 Z에서 어느 부분에 속하는 지를 찾아 수를 더해주고,
한 단계씩 작은 Z로 내려가면서 이 과정을 반복해주면 된다.

package day0215;

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

public class Z {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;

	static int find(int level, int x, int y, int out) {
		if (level == 0)
			return out;
		int N = (int) Math.pow(2, level);
		if (x >= N / 2) {
			out += N * N / 2;
			x -= N / 2;
		}
		if (y >= N / 2) {
			out += N * N / 4;
			y -= N / 2;
		}
		return find(level - 1, x, y, out);
	}

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int N, R, C; // 행과 열을 담을 예정.
		st = new StringTokenizer(br.readLine(), " ");
		
		N = Integer.parseInt(st.nextToken());
		R = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		
		System.out.println(find(N, R, C, 0));
	}
}
profile
SSAFY 7기

0개의 댓글