알고리즘 풀이/프로그래머스

[프로그래머스][JAVA] 예상 대진표 (시뮬레이션..?)

배게 2022. 3. 25. 23:01
728x90

다른 사람의 풀이

class Solution
{
    public int solution(int n, int a, int b)
    {
        return Integer.toBinaryString((a-1)^(b-1)).length();
    }
}

 

 

 

 

더 간략하게

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution
{
    public int solution(int n, int a, int b)
    {
        int answer = 0;
        while(a!=b){
            a = (a+1)/2;
            b = (b+1)/2;
            answer++;
        }
 
        return answer;
    }
    
}
cs

 

 

 

 

맨 처음 푼 방식

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution
{
    public int solution(int n, int a, int b)
    {
        int answer = 1;
        while(!isMeet(a,b)){
            a = (a+1)/2;
            b = (b+1)/2;
            answer++;
        }
 
        return answer;
    }
    
    private boolean isMeet(int a, int b){
        boolean ret = ((a+1)/2==(b+1)/2)?true:false;
        return ret;
    }
}
cs