[BaekJoon] 9366번 - 삼각형 분류 [Java][C++]
[BaekJoon] 9366번 - 삼각형 분류 [Java][C++]
1. 문제 풀이
세 변이 주어졌을 때, 삼각형을 분류하는 문제로 먼저 가장 긴 변이 다른 두 변의 합보다 크거나 같은지 여부로 삼각형이 될 수 있는지를 조건 분기해줬고, 이후 세 변의 길이가 모두 같은지, 길이가 같은 두 변이 있는지로 분기해줬다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= t; tc++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (a >= b + c || b >= a + c || c >= a + b) {
sb.append("Case #").append(tc).append(": invalid!\n");
} else if (a == b && b == c) {
sb.append("Case #").append(tc).append(": equilateral\n");
} else if (a == b || b == c || c == a) {
sb.append("Case #").append(tc).append(": isosceles\n");
} else {
sb.append("Case #").append(tc).append(": scalene\n");
}
}
System.out.println(sb);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
int a, b, c;
cin >> a >> b >> c;
if (a >= b + c || b >= a + c || c >= a + b) {
cout << "Case #" << tc << ": invalid!\n";
} else if (a == b && b == c) {
cout << "Case #" << tc << ": equilateral\n";
} else if (a == b || b == c || c == a) {
cout << "Case #" << tc << ": isosceles\n";
} else {
cout << "Case #" << tc << ": scalene\n";
}
}
}
This post is licensed under CC BY 4.0 by the author.