[BaekJoon] #10101 - 삼각형 외우기 [Java][C++]
[BaekJoon] #10101 - 삼각형 외우기 [Java][C++]
1. 아이디어
먼저 삼각형이 될 수 있는지 없는지 조건문으로 판단한 후 삼각형이 될 수 있으면 정삼각형인지, 이등변삼각형인지 판단하는 방식으로 해결했다.
삼각형이 되려면 세 각의 합이 $180^\circ$여야 하고, 정삼각형은 세 각이 모두 $60^\circ$, 이등변삼각형은 두 각이 같은 경우가 존재하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(1)$ | $O(1)$ |
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a1 = Integer.parseInt(br.readLine());
int a2 = Integer.parseInt(br.readLine());
int a3 = Integer.parseInt(br.readLine());
if (a1 + a2 + a3 == 180) {
if (a1 == 60 && a2 == 60 && a3 == 60) {
System.out.println("Equilateral");
} else if (a1 == a2 || a2 == a3 || a3 == a1) {
System.out.println("Isosceles");
} else {
System.out.println("Scalene");
}
} else {
System.out.println("Error");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a1, a2, a3;
cin >> a1 >> a2 >> a3;
if (a1 + a2 + a3 == 180) {
if (a1 == 60 && a2 == 60 && a3 == 60) {
cout << "Equilateral";
} else if (a1 == a2 || a2 == a3 || a3 == a1) {
cout << "Isosceles";
} else {
cout << "Scalene";
}
} else {
cout << "Error";
}
}
This post is licensed under CC BY 4.0 by the author.