티스토리 뷰

https://www.acmicpc.net/problem/16953

 

16953번: A → B

첫째 줄에 A, B (1 ≤ A < B ≤ 109)가 주어진다.

www.acmicpc.net

const fs = require('fs');
let [A,B] = fs.readFileSync("./dev/stdin").toString().trim().split(" ").map(Number)
let answer;
let cnt = 1;
while(A<B){
  const S = String(B);
  if(S[S.length-1]=='1'){
    const C = +S.substring(0,S.length-1)
    if(C>=A){
      B = C
      cnt++;
    }else{
      answer = -1;
      break;
    }
  }else{
    if(B%2==0 && B/2>=A){
      B/=2;
      cnt++;
    }else{
      answer = -1;
      break;
    }
  }

  if(A==B){
    answer =cnt;
    break; 
  }else if(A>B){
    answer = -1; 
    break;
  }
}
console.log(answer)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함