[ 다먹살 ]/- Coding

[구름] 레벨2 두 점 사이의 거리

엉망으로살기 2021. 8. 27. 10:20
반응형

https://level.goorm.io/exam/43228/%EB%91%90-%EC%A0%90-%EC%82%AC%EC%9D%B4%EC%9D%98-%EA%B1%B0%EB%A6%AC/quiz/1

 

구름LEVEL

코딩테스트에서 가장 높은 비중을 차지하는 알고리즘 문제를 제작하고 풀이할 수 있는 온라인 저지 서비스입니다. 기업에서 선호하는 C, C++, 파이썬(Python), 자바(Java), 자바스크립트(Javascript) 이

level.goorm.io

 

String.format 메소드를 이용해서 소수점 처리를 처음 사용해봤다. C에서는 출력문 안에 %.2f 이런 식으로 사용이 가능했었는데, 자바에서도 같은 처리방식을 찾았다. 예전에 이 소수점 자리 때문에 비슷한 문제를 해결 못한 적이 있었는데, 유용하게 사용할 수 있을 것 같다.

 

문제

 

 

코드

import java.util.Scanner;

class Main
{
     public static void main(String[] args) throws Exception
     {
         Scanner sc = new Scanner(System.in);
         int firstX = sc.nextInt();
         int firstY = sc.nextInt();
         int nextX = sc.nextInt();
         int nextY = sc.nextInt();

         System.out.println(String.format("%.2f", Math.sqrt(Math.pow(nextX-firstX, 2)+Math.pow(nextY-firstY, 2))));
     }
}

 

반응형