백준 4153번 : 직각삼각형[Java]

문제 출처

www.acmicpc.net/problem/4153

 

4153번: 직각삼각형

입력은 여러개의 테스트케이스로 주어지며 마지막줄에는 0 0 0이 입력된다. 각 테스트케이스는 모두 30,000보다 작은 양의 정수로 주어지며, 각 입력은 변의 길이를 의미한다.

www.acmicpc.net

 

import java.util.Scanner;
public class d024_Q4153 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		
		while(a != 0) {
			int temp;
			if(a > c) {
				if(a > b) {
					temp = a;
					a = c;
					c = temp;
				}
			}
			if(b > c) {
				if(b > a) {
					temp = b;
					b = c;
					c = temp;
				}
			}
			
			if(a* a + b * b == c * c) {
				System.out.println("right");
			}
			else {
				System.out.println("wrong");
			}
			a = sc.nextInt();
			b = sc.nextInt();
			c = sc.nextInt();
		}
		sc.close();
	}

}

 

입력되는 세 숫자 중 어떤게 가장 긴 변인지 알 수 없으므로 먼저 긴변이 어떤것인지 부터 찾았다.

c에 가장 긴변을 가져다 놨다.

 

근데 꼭 이렇게 앞에서 if문을 사용할게 아니라 

import java.util.Scanner;
public class d024_Q4153 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		
		while(a != 0) {
			if(a* a + b * b == c * c) {
				System.out.println("right");
			}
			else if(a * a + c * c == b * b) {
				System.out.println("right");
			}
			else if(b * b + c* c == a * a) {
				System.out.println("right");
			}
			else {
				System.out.println("wrong");
			}
			a = sc.nextInt();
			b = sc.nextInt();
			c = sc.nextInt();
		}
		sc.close();
	}

}

더 빠르지 않을까?해서 작성해서 채점을 돌려봤는데

음 전자가 더 속도가 빨랐다. 이유가 뭔지 알고 싶다... if문 안의 연산이 여러번 반복되기 때문일까?

myoskin