[백준] 5073번: 삼각형과 세 변

2022. 12. 8. 15:49TIL💡/Algorithms

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

 

5073번: 삼각형과 세 변

각 입력에 맞는 결과 (Equilateral, Isosceles, Scalene, Invalid) 를 출력하시오.

www.acmicpc.net

가볍게 브론즈로 풀었다. 대신 문제를 어떻게 간결하게 논리적으로 깔끔하게 풀 수 있는가에 대해서는 충분히 고민해보면 좋은 문제이다.

그리고 입력되는 라인 수가 정해져 있지 않으므로 이에 대해서도 잘 정리해볼 수 있는 문제였다.

 

항상 string 파싱할 때 substr가 유용하므로 사용법을 익혀둘 필요가 있다.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int a,b,c;
vector<int> parsed_nums(string str) {
    vector<int> result;

    int idx = str.find(" ");
    int a = stoi(str.substr(0, idx));
    str = str.substr(idx + 1);
    idx = str.find(" ");
    int b = stoi(str.substr(0, idx));
    str = str.substr(idx + 1);
    idx = str.find(" ");
    int c = stoi(str);

    result.push_back(a);
    result.push_back(b);
    result.push_back(c);

    return result;
}

void judge(vector<int> lines) {
    int a = lines[0];
    int b = lines[1];
    int c = lines[2];

    if(a + b <= c) {
        cout << "Invalid" << '\n';
    }
    else if(a == b && b == c) {
        cout << "Equilateral" << '\n';
    }
    else if(a == b || b == c) {
        cout << "Isosceles" << '\n';
    }
    else {
        cout << "Scalene" << '\n';
    }
}
int main() {
    string str;
    while(getline(cin, str)) {
        if(str == "0 0 0") break;
        vector<int> lines = parsed_nums(str);
        sort(lines.begin(), lines.end());
        judge(lines);
    }
}