자바로 1064 풀기

hong030·2023년 6월 25일
0
  • 실버 4단계 문제

풀이)
세 점이 일직선에 놓였을 경우엔 -1을 출력.
AB 길이, BC 길이, CA길이를 모두 구하고 세 선분 중 가장 큰 선분, 가장 작은 선분의 차를 이용해 답을 출력한다. 둘레의 길이이므로 *2를 해야한다.

내 코드)

import java.io.*;
import java.util.*;
 
public class Main {
   public static void main(String[] args) throws IOException {
      StringTokenizer st = new StringTokenizer(new BufferedReader(new InputStreamReader(System.in)).readLine());
      int[] points = new int[6];
      for(int i=0;i<6;i++) points[i] = Integer.parseInt(st.nextToken());
 
      if((points[2]-points[0])*(points[5]-points[1])==(points[4]-points[0])*(points[3]-points[1])){
         System.out.println(-1);
         return;
      }
 
      double ab = getLength(points[0],points[1],points[2],points[3]);
      double bc = getLength(points[2],points[3],points[4],points[5]);
      double ca = getLength(points[4],points[5],points[0],points[1]);
 
      double max = Math.max(ab,Math.max(bc,ca));
      double min = Math.min(ab,Math.min(bc,ca));
 
      System.out.println(2*(max-min));
 
   }
   public static double getLength(double x1, double y1, double x2, double y2){
      return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
   }
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글