JAVA/Coding Test

[JAVA] 백준 14215 세 막대_기하: 직사각형과 삼각형

오늘도개발 2023. 4. 13. 14:40

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

 

14215번: 세 막대

첫째 줄에 a, b, c (1 ≤ a, b, c ≤ 100)가 주어진다.

www.acmicpc.net

 

 접근 :

 

 - 길이 3개를 입력 받는다.

 

 - 길이를 정렬하여 가장 큰 길이의 수를 찾는다.

 

 - 나머지 두 변의 길이의 합 < 가장 큰 길이 조건에 해당하면 그대로 둘레를 구하고 조건에 해당하지 않으면

   가장 큰 길이의 변 = 나머지 두변의 길이의 합 - 1 로 수정하여 둘레를 구한다.

 

 코드 구현 : 

 

import java.io.*;

public class Main {

	static class Triangle {
		private int a;
		private int b;
		private int c;

		// 가장 긴 변이 c
		public Triangle(int a, int b, int c) {
			if (a > b) {
				this.b = b;
				if (a > c) {
					this.c = a;
					this.a = c;
				} else {
					this.c = c;
					this.a = a;
				}
			} else {
				this.a = a;
				if (b > c) {
					this.c = b;
					this.b = c;
				} else {
					this.c = c;
					this.b = b;
				}
			}
			this.convertC();
		}

		private void convertC() {
			int sum = this.a + this.b;
			this.c = (this.c >= sum) ? (sum - 1) : this.c;
		}

		public int getPerimeter() {
			return (this.a + this.b + this.c);
		}
	}

	public static void main(String[] srgs) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] inputs = br.readLine().split(" ");
		int[] inputs_num = new int[3];
		for (int i = 0; i < inputs_num.length; i++) {
			inputs_num[i] = Integer.parseInt(inputs[i]);
		}

		Triangle triangle = new Triangle(inputs_num[0], inputs_num[1], inputs_num[2]);

		System.out.println(triangle.getPerimeter());
		br.close();
	}
}