백준 4153번 : 직각삼각형[Java]
문제 출처
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문 안의 연산이 여러번 반복되기 때문일까?
'문제풀이 > 코딩테스트' 카테고리의 다른 글
백준 1874번 : 스택 수열 [C언어] (0) | 2021.03.16 |
---|---|
백준 3053번 : 택시 기하학[Java] (0) | 2020.09.26 |
백준 3009번 : 네 번째 점[Java] (0) | 2020.09.24 |
백준 1085번 : 직사각형에서 탈출[Java] (0) | 2020.09.23 |
백준 9020번 : 골드바흐의 추측[Java] (0) | 2020.09.21 |