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();
}
}
'JAVA > Coding Test' 카테고리의 다른 글
[JAVA] 백준 4673 셀프 넘버_브루트포스 (0) | 2024.02.04 |
---|---|
[JAVA] 백준 7785 회사에 있는 사람_집합과 맵 (0) | 2023.04.17 |
[JAVA] 백준 2903 중앙 이동 알고리즘_일반 수학1 (0) | 2023.04.13 |
[JAVA] 백준 2720 세탁소 사장 동혁_일반 수학1 (0) | 2023.04.13 |
[JAVA] 백준 11005 진법 변환 2_일반 수학1 (0) | 2023.04.12 |