*주의* int형이 아니라 long형으로 설정해야한다!
계속 시간초과가 떠서 무슨 문제인가했더니...
result,x변수의 값이 int형의 범위에 초과될 수 있어서 그런것이었다!
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); // 테스트 개수
for (int i = 0; i < t; i++) {
long x = sc.nextInt(); //현재 위치
long y = sc.nextInt(); //목표 위치
long result = 0;//작동 횟수의 최소값
while (true) {
result++;
if (y <= x + result) {
result = (result-1)*2 + 1;
break;
}else if(y<= (x + result*2)){
result = result*2;
break;
}
x= x+result*2;
}
System.out.println(result);
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 10250 ACM 호텔 자바 (0) | 2019.11.06 |
---|---|
백준 2869 달팽이는 올라가고 싶다 자바 (0) | 2019.11.04 |
백준 1193 분수 찾기 자바 (0) | 2019.10.31 |
백준 2292 벌집 자바 (0) | 2019.10.30 |
백준 2839 설탕 배달 자바 (0) | 2019.10.29 |
댓글