본문 바로가기

백준 문제풀기/JAVA

[백준 2022 JAVA 자바] 사다리

어? 단순한 삼각비와 피타고라스 문제잖아?

했다가 저 처럼 고생하지 마시고

 

컴퓨터가 푸는 문제니깐

0.001의 오차를 생각해서

반복문을 돌립시다

 

코드입니다

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        double x = Double.parseDouble(st.nextToken());
        double y = Double.parseDouble(st.nextToken());
        double c = Double.parseDouble(st.nextToken());

        double left = 0;
        double right = Math.min(x, y);

        while (right - left >= 0.001) {
            double width = (left + right) / 2;
            double h1 = Math.sqrt(Math.pow(x, 2) - Math.pow(width, 2));
            double h2 = Math.sqrt(Math.pow(y, 2) - Math.pow(width, 2));
            double res = (h1 * h2) / (h1 + h2);
  
            if (res >= c) {
            	left = width;
            }else {
            	right = width;
            }
        }
        System.out.println(String.format("%.3f", right));
    }
}