#유형 : 시뮬레이션 

# 토너먼트가 진행할때마다 홀수인 경우에는 num/2 + 1 짝수일 경우에는 num/2 로 진행된다. 그러면 타겟끼리 같은 그룹인지 어떻게 체크하냐면 예를들어 1 2 3 이런 경우에 +1하고 /2를 해주면 그룹핑이 가능해진다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package bj;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class p1057 {
    static int N,A,B;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        A = Integer.parseInt(st.nextToken());
        B = Integer.parseInt(st.nextToken());
        int cnt = 1;
        while(true) {
            int rs1 = solve(A);
            int rs2 = solve(B);
            
            if(rs1==rs2)
                break;
            
            if(A%2==0)
                A/=2;
            else if(A%2==1) {
                A/=2;
                A++;
            }
            if(B%2==0)
                B/=2;
            else if(B%2==1) {
                B/=2;
                B++;
            }
            
            cnt++;
        }
        System.out.println(cnt);
    }
    public static int solve(int num) {
        num+=1;
        return num/2;
    }
}
 
cs

+ Recent posts