99클럽 코테 스터디 15일차 TIL Divisor Game

방지환·2024년 6월 9일

코테 스터디

목록 보기
20/37

Divisor Game

  • 문제 풀이

    1. Alice와 Bob이 주어진 n값에 대해 n % x == 0 인 x를 선택한 후 n-x를 하여 계속 이어나가며 마지막까지 움직일 수 없을때 까지 구하는 문제이다.
    2. 주어진 예제에서 n이짝수일때 Alice가 홀수를 만들어 이겨야 하며 n이 홀수 일때 Bob이
      홀수가 되며 이기게 된다.
    • If the current number is even, we can always subtract a 1 to make it odd. If the current number is odd, we must subtract an odd number to make it even. 이것이 힌트가 되었다.
    1. 주어진 것을 생각하면 단순히 return n%2==0;하여 생각하면 되는 문제였다.
  • 풀이 소스

class Solution {
    public boolean divisorGame(int n) {
        return n%2==0;
    }
}
  • 참고한 풀이 소스

class Solution {
    public boolean divisorGame(int n) {
        if(n<=1) return false;
        
        for(int x=1;x<n;x++){
            if(n%x==0){
                if(!divisorGame(n-x)) return true;
            }
        }
        return false;
    }
}

0개의 댓글