https://www.acmicpc.net/problem/5073
while문을 돌면서 조건을 검사하고 그에 맞는 결과를 출력해주면 된다. 삼각형의 조건을 제일 먼저 검사해주면 되는데 배열을 선언해주고 이를 정렬해서 마지막 값과 앞의 두 값의 합을 비교해주면 된다. 나머지도 조건대로 해주면 된다.
정답코드
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a, b, c;
while (true) {
cin >> a >> b >> c;
// 종료 조건
if (a == 0 && b == 0 && c == 0) break;
// 세 변을 정렬하여 가장 긴 변이 마지막에 오도록 정렬
int sides[3] = {a, b, c};
sort(sides, sides + 3);
// 삼각형의 조건 검사: 가장 긴 변이 다른 두 변의 합보다 작아야 함
if (sides[2] >= sides[0] + sides[1]) {
cout << "Invalid" << endl;
} else if (sides[0] == sides[1] && sides[1] == sides[2]) {
cout << "Equilateral" << endl;
} else if (sides[0] == sides[1] || sides[1] == sides[2]) {
cout << "Isosceles" << endl;
} else {
cout << "Scalene" << endl;
}
}
return 0;
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준][C++]2292번. 벌집 (0) | 2024.09.11 |
---|---|
[백준][C++]14215번. 세 막대 (1) | 2024.09.11 |
[백준][C++]10101번. 삼각형 외우기 (0) | 2024.09.09 |
[백준][C++]9063번. 대지 (0) | 2024.09.09 |
[백준][C++]3009번. 네 번째 점 (1) | 2024.09.06 |