코딩테스트/프로그래머스

[Level 2][C++] 최댓값과 최솟값

MJ.Lee 2024. 10. 10. 22:32
#include <string>
#include <vector>
#include <sstream>  // streamstring의 헤더
#include <algorithm>

using namespace std;

string solution(string s) {
    vector<int> nums;
    
    stringstream ss(s);  // 문자열을 stringstream에 집어넣음.
    string numString;
    while(ss>>numString) //공백 기준으로 쪼갠다.
    {
        nums.push_back(stoi(numString)); //문자열을 int로 변환해서 배열에 집어넣는다. 
    }
    
    sort(nums.begin(), nums.end());  //오름차순으로 정렬
    //int를 string으로 변환
    string answer = to_string(nums.front())+" "+to_string(nums.back()); 
    return answer;
}