[Baekjoon][Java] Fly me to the Alpha Centauri

HyeBin, Park·2022년 3월 4일
0

Baekjoon

목록 보기
7/11
post-thumbnail

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

📒 문제

📒 입출력

🐣 코드

import java.util.Scanner;

public class Flyme_1011 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long T = sc.nextLong();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < T; i++) {
            long start = sc.nextLong();
            long end = sc.nextLong();
            long distance = end - start;
            long max = (long)Math.sqrt(distance);
            if (max == Math.sqrt(distance))
                sb.append((max * 2) - 1);
            else if(distance <= max * max + max)
                sb.append(max * 2);
            else
                sb.append((max * 2) + 1);
            if(i < T-1)
                sb.append("\n");
        }
        System.out.println(sb);
    }
}

🔍 정리하기

처음에는 맨마지막이 1칸으로만 가야한다는 조건을 생각하지 못하고 n + 1 로 갈 수 있다면 무조건 n+1로 갔다. 이 조건부터 다시 생각하여 움직이는 거리가 반으로 나뉠수 있다는 점을 알게되었고 (1,2,3,2,1) 이를 생각하며 코드를 다시 작성하였다. 자료형 조건을 자세히 봐야겠다.

0개의 댓글