πŸ’‘ν’€μ΄λ°©μ•ˆ


2 와 162κ°€ μ£Όμ–΄μ‘Œμ„ 경우 λ’€μ—μ„œλΆ€ν„° 연산을 μ‹œμž‘ν•΄λ³΄λ©΄
162 <= 81 -> 81 <= 8 -> 8 <= 4 -> 4 <= 2
λ‹€μŒκ³Ό 같은 κ²°κ³Όλ₯Ό 얻을 수 μžˆλ‹€.

public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int a = Integer.parseInt(st.nextToken());
        int b = Integer.parseInt(st.nextToken());

        boolean stop = true;
        int cnt = 0;
        while (stop){
            if (b%10==1){
                b /= 10;
                cnt++;
            }else if (b%2 == 0){
                b /= 2;
                cnt++;
            }else{
                cnt =-1;
                stop = false;
            }


            if (b == a){
                stop = false;
                cnt++;
            }else if (b < a){
                cnt =-1;
                stop = false;
            }
        }
        System.out.println(cnt);
    }
profile
Back-End Developer

0개의 λŒ“κΈ€