[프로그래머스 lev2/JS] 예상 대진표

woolee의 기록보관소·2022년 11월 8일
0

알고리즘 문제풀이

목록 보기
86/178

문제 출처

프로그래머스 lev2 - 예상 대진표

문제

나의 풀이

1차 시도 (88.2/100)

function solution(n,a,b) {
  let answer=1;
  
  if (a<b) {
    // a+1 !== b
    while (1) {
      if (a==1 && b==2) break;
      if (a+1 == b) break;
      a=Math.ceil(a/2);
      b=Math.ceil(b/2);
      answer++;
    }
  }
  else {
    while (1) {
      if (a==2 && b==1) break;
      if (a==b+1) break;
      a=Math.ceil(a/2);
      b=Math.ceil(b/2);
      answer++;
    }
  }
  return answer;
}

console.log(solution(8,4,7));

다른 풀이

a와 b가 붙게 되는 순간, 즉 같은 번호로 한쌍이 되는 순간까지 라운드를 진행해야 함.
무조건 올림처리를 해주기 때문에, 둘의 값이 같아지는 경우가 존재한다.

function solution(n,a,b) {
  let answer=1;
  
  if (a<b) {
    while (1) {
      if (Math.ceil(a/2) == Math.ceil(b/2)) break;
      a=Math.ceil(a/2);
      b=Math.ceil(b/2);
      answer++;
    }
  }
  else {
    while (1) {
      if (Math.ceil(a/2)==Math.ceil(b/2)) break;
      a=Math.ceil(a/2);
      b=Math.ceil(b/2);
      answer++;
    }
  }
  return answer;
}

console.log(solution(8,4,7));
function solution(n,a,b)
{
    let answer = 0;
    while(a !== b) {
        a = Math.ceil(a/2);
        b = Math.ceil(b/2);
        answer++;
    }

    return answer;
}
profile
https://medium.com/@wooleejaan

0개의 댓글