알고리즘/Baekjoon
Fly me to the Alpha Centauri _1011
개굴이모자
2019. 1. 27. 01:53
반응형
BaekJoon/BOJ [JAVA] Fly me to the Alpha Centauri _1011
HINT: 1 121 12321 1234321
1 4 9 16 25 36 49
+ 1 3 5 7 9 11 13...
3의 제곱이면 거리가, 3번째 홀수인 5번 공간이동한다. 이때 나누어 떨어지지 않는 경우를 고려해 나머지에 대해서 모자란 만큼을 끼워 넣어주면 된다.
count/2+1인 이유는 예를 들어 11count면, 12345654321 이기에 6까지 중에서 더해봐야 하기 때문이다.
import java.util.Scanner;
public class baekjoon_1011 {
public static void main(String[] argc) {
Scanner scanner = new Scanner(System.in);
int iter = Integer.parseInt(scanner.nextLine());
for(int i=0;i<iter;i++){
int x = scanner.nextInt();
int y = scanner.nextInt();
int distance = y-x;
int count = (int)Math.sqrt(distance);
int tmp = (int)Math.pow(count,2);
count=1+(2*(count-1)); //count번째 홀수 구하기
System.out.println(leftover(distance-tmp,count));
}
}
static public int leftover(int leftover, int count) {
int lo_count=0;
for (int i = (count/2+1); i > 0; i--) {
if(leftover>=i) {
leftover-=i;
lo_count++;
i++;
}
}
return count+lo_count;
}
}
반응형