본문 바로가기
알고리즘 & 자료구조/프로그래머스

예상 대진표

by 신재권 2023. 1. 7.
package programmers;

public class 예상대진표 {

   public static int solution(int n, int a, int b) {
      int round = 1;

      while (Math.abs(a - b) != 1 || a / 2 == b / 2) {
         a = nextNumber(a);
         b = nextNumber(b);
         round++;
      }

      return round;
   }

   public static int nextNumber(int x) {
      if (x % 2 == 1) {
         x = x / 2 + 1;
      } else {
         x /= 2;
      }
      return x;
   }

   public static void main(String[] args) {
      System.out.println(solution(8, 4, 7));
      System.out.println(solution(8, 4, 5));
   }
   
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

멀리 뛰기  (0) 2023.01.10
점프와 순간이동  (0) 2023.01.08
N개의 최소공배수  (0) 2023.01.06
구명 보트  (0) 2023.01.05
영어 끝말잇기  (0) 2023.01.04