JAVA/Coding Test

[JAVA] 백준 5073 삼각형과 세 변_기하

오늘도개발 2023. 3. 23. 16:02

https://www.acmicpc.net/problem/5073

 

5073번: 삼각형과 세 변

각 입력에 맞는 결과 (Equilateral, Isosceles, Scalene, Invalid) 를 출력하시오.

www.acmicpc.net

 

 접근 : 

 

 - 0 0 0 을 입력 받기 전 까지 3 변의 길이를 입력받는다.

 

 - 제일 긴변의 길이가 나머지 2변의 길이의 합보다 같거나 크면 Invalid 출력\

 

 - 세변의 길이가 모두 같으면 Equilateral 출력

 

 - 두변의 길이가 같으면 Isosceles 출력

 

 - 세변의 길이가 모두 다르면 Scalene 출력

 

 

 코드 구현 : 

 

import java.io.*;

public class Main {

	static class Triangle {
		private int[] lengths;

		public Triangle() {

		}

		public void setAngles(int[] lengths) {
			this.lengths = lengths;
		}

		public String CheckTriangle() {

			// 삼각형의 조건을 만족하지 못하는 경우
			if (lengths[0] >= lengths[1] + lengths[2] || lengths[1] >= lengths[0] + lengths[2]
					|| lengths[2] >= lengths[0] + lengths[1]) {
				return "Invalid";
			

			// 세변의 길이가 모두 같은 경우
			}else if ((lengths[0] == lengths[1]) && (lengths[0] == lengths[2]) && (lengths[1] == lengths[2])) {

				return "Equilateral";

			// 두변의 길이가 같은 경우
			} else if ((lengths[0] == lengths[1]) || (lengths[0] == lengths[2]) || (lengths[1] == lengths[2])) {
				return "Isosceles";
			}
			
			// 세변의 길이가 모두 다른 경우
			return "Scalene";
		}
	}

	public static void main(String[] srgs) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();

		int n = 3;
		int[] angles = new int[n];
		String[] inputs;
		Triangle triangle = new Triangle();

		while (true) {
			inputs = br.readLine().split(" ");
			if ((inputs[0].equals("0") && inputs[1].equals("0") && inputs[2].equals("0"))) {
				break;
			} else {
				for (int i = 0; i < n; i++) {
					angles[i] = Integer.parseInt(inputs[i]);
				}
				triangle.setAngles(angles);
				sb.append(triangle.CheckTriangle()).append("\n");
			}
		}
		System.out.println(sb);
		br.close();
	}
}