#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string s) {
    vector<int>v;
    int num = 0;
    for(int i = size(s)-1; i>=0; --i){
        if(isdigit(s[i])) num = int(s[i])-'0';
        else{
            if(s[i] == '-') num *= -1;
            if(s[i] == ' '){
                v.push_back(num);
                num = 0;
            }
        }       
        if(i==0) v.push_back(num);
    }
    
    sort(v.begin(), v.end());
    
    string answer = to_string(v[0]);
    answer += " ";
    answer += to_string(v[v.size()-1]);
    return answer;
}

연습문제는 다 맞는데 제출하면 다 틀렸다고 나온다

왜...?!?!?

두자리수 이상을 고려하지 않고 풀었다

이걸 반영하면

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string s) {
    vector<int>v;
    string t = "";
    for(auto x : s){
        if(x == ' ') {
            v.push_back(stoi(t));
            t="";
        }
        else t += x;       
    }
    v.push_back(stoi(t));
    
    sort(v.begin(), v.end());
    
    string answer = to_string(v[0]) + " " + to_string(v[v.size()-1]);
    return answer;
}

이렇게 바꾸면... 성공!

+ Recent posts