BOJ 5904 Moo 게임

less than 1 minute read

  1. 변수

    int N //출력할 문자의 자리
    int[] mooLen //moo게임 하는동안 길이 구할수 잇다
    
  2. 로직

    • mooLen 배열을 채운다(최대 10번)

    • 재귀를 통해 앞 + 새로 만든 moo + 뒤 중에 위치를 구해 구한다

  3. 코드

package com.ssafy;

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

public class BOJ_5904_Moo게임_201229 {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

		int[] mooLen = new int[32];

		int index = calMoolen(mooLen, N);
		
		System.out.println(dfs(index, N, mooLen));
	}

	private static String dfs(int index, int n, int[] mooLen) {
		if(index == 0) {
			if(n == 1)
				return "m";
			else
				return "o";
		}
		
		if(mooLen[index-1] >= n)
			return dfs(index-1, n, mooLen);
		else if(mooLen[index-1] + index + 3 >= n) {
			if(n == mooLen[index-1] + 1)
				return "m";
			else
				return "o";
		}else {
			return dfs(index-1, n-(mooLen[index-1] + index + 3), mooLen);
		}
		
	}

	private static int calMoolen(int[] mooLen, int n) {
		mooLen[0] = 3;
		for (int i = 1; i < mooLen.length; i++) {
			mooLen[i] = mooLen[i - 1] * 2 + (i + 3);
			if(mooLen[i] > n)
				return i;
		}
		return mooLen.length;
	}

}

Leave a comment