![[백준] 4153 직각삼각형 - JAVA](/images/blog/4153-java/image-01.png)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.util.Arrays.sort;
public class Main {
static int[] list;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
while (true){
st = new StringTokenizer(br.readLine());
list = new int[3];
list[0] = Integer.parseInt(st.nextToken());
list[1] = Integer.parseInt(st.nextToken());
list[2] = Integer.parseInt(st.nextToken());
sort(list);
if(list[0] == 0 && list[1] == 0 && list[2] == 0){
return;
}
if(Math.pow(list[0], 2) + Math.pow(list[1], 2) == Math.pow(list[2], 2)){
System.out.println("right");
} else{
System.out.println("wrong");
}
}
}
}
후기
기본기를 다지기 위해 쉬운 문제부터 다시 시작하고 있다.
이번 문제는 단순한 직각삼각형 문제
내가 처음 짠 위 코드에 개선사항이 세개가 있었는데
list 지역변수로 사용하기, 정렬보다 조건 검사를 먼저 하기, 튜플 사용하기이다.
개선한 코드는 다음과 같다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
// 종료 조건 먼저 확인
if (a == 0 && b == 0 && c == 0) break;
int[] sides = {a, b, c};
Arrays.sort(sides);
if (sides[0] * sides[0] + sides[1] * sides[1] == sides[2] * sides[2]) {
System.out.println("right");
} else {
System.out.println("wrong");
}
}
}
}