📌
wooaoe TIL
  • ICT 란?
  • ORACLE DB SQL
  • 백준 #2581번 JAVA 풀이
  • DEVEIW 2019 컨퍼런스 후기
  • 백준 #4153번 직각삼각형JAVA 풀이
  • Cloud Computing 과 AWS
  • JAVA
  • HTML
    • HTML Day 2
    • HTML Day 1
  • Trend & New Tech
  • SQL
Powered by GitBook
On this page
  • 4153번 직각삼각형 만들기
  • ArrayList를 활용한 풀이

Was this helpful?

백준 #4153번 직각삼각형JAVA 풀이

4153번 직각삼각형 알고리즘

PreviousDEVEIW 2019 컨퍼런스 후기NextCloud Computing 과 AWS

Last updated 5 years ago

Was this helpful?

4153번 직각삼각형 만들기

과거 이집트인들은 각 변들의 길이가 3, 4, 5인 삼각형이 직각 삼각형인것을 알아냈다. 
주어진 세변의 길이로 삼각형이 직각인지 아닌지 구분하시오.

입력은 여러개의 테스트케이스로 주어지며 마지막줄에는 0 0 0이 입력된다. 각 테스트케이스는 모두 30,000보다 작은 양의 정수로 주어지며, 각 입력은 변의 길이를 의미한다. 각 입력에 대해 직각 삼각형이 맞다면 "right", 아니라면 "wrong"을 출력한다.

MaM

Source Code

package com.baekjoon;

import java.util.Scanner;

public class Triangle {
	public void test1() {
		Scanner sc = new Scanner(System.in);

		int a, b, c;

		while (true) {
			System.out.print("정수 a : ");
			a = sc.nextInt();
			System.out.print("정수 b : ");
			b = sc.nextInt();
			System.out.print("정수 c : ");
			c = sc.nextInt();

			if (a > 0 && a <= 30000 && b > 0 && b <= 30000 && c > 0 && c <= 30000) {

				int a2 = (int) Math.pow(a, 2);
				int b2 = (int) Math.pow(b, 2);
				int c2 = (int) Math.pow(c, 2);

				if (a2 == b2 + c2) {
					System.out.println("right");
				} else if (b2 == a2 + c2) {
					System.out.println("right");
				} else if (c2 == a2 + b2) {
					System.out.println("right");
				} else {
					System.out.println("wrong");
				}
			} else {
				System.out.println("모든 정수는 0보다 커야하고, 30000보다 작아야 합니다.");
			}
			if(a == 0 && b == 0 && c == 0) {
				break;
			}
		}

	}

}
public static void arrlist() {
      Scanner sc = new Scanner(System.in);
      
      ArrayList<Integer> alist = new ArrayList<Integer>();
      ///alist.size()
      
      for (int i = 0; i < 3; i++) {
         System.out.println("정수를 입력하세요.");
         
         int a = sc.nextInt();
         
         if (a >= 0 && a <= 30000) {
            alist.add(a);
         }
         
         Collections.sort(alist);
         
      }
      
      if (alist.get(0) * alist.get(0) + alist.get(1) * alist.get(1) == alist.get(2) * alist.get(2)) {
         System.out.println("right");
      } else {
         System.out.println("wrong");
      }
      
   }

ArrayList를 활용한 풀이

친구가 푼 방식인데 if문을 남발 하는 것 보다 ArrayList와 Collection을 활용하는 것만으로도 코드가 매우 간결해진다는 것을 알았다. 앞으로도 간결한 코드에 대해서 더욱 고민해보는 사람이 되길!

📌
🔥
🔥
⚙️
🌟