Algorithm/Baekjoon
[백준] 4153번 직각삼각형(JAVA)
다교이
2022. 7. 22. 20:19
728x90
문제
- 주어진 세변의 길이로 삼각형이 직각인지 아닌지 구분
해결방법
피타고라스 정리만 알면 문제를 쉽게 해결할 수 있다.
피타고라스 정리 : 직각삼각형에서, 빗변 길이의 제곱은 빗변을 제외한 두 변의 각각 제곱의 합과 같다.
코드
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String triangle = br.readLine();
StringTokenizer st = new StringTokenizer(triangle, " ");
if(triangle.equals("0 0 0")) break; // 0 0 0이 입력으로 들어올 때 종료
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
// 빗변이 C일 때
if(Math.pow(C, 2) == Math.pow(A, 2) + Math.pow(B, 2)) System.out.println("right");
// 빗변이 A일 때
else if(Math.pow(A, 2) == Math.pow(B, 2) + Math.pow(C, 2)) System.out.println("right");
// 빗변이 B일 때
else if(Math.pow(B, 2) == Math.pow(A, 2) + Math.pow(C, 2)) System.out.println("right");
else System.out.println("wrong");
}
br.close();
}
}
728x90