Algorithm/프로그래머스
[프로그래머스] 최댓값과 최솟값 with C++
nowkoes
2023. 5. 29. 00:00
문제 설명
제한 사항 및 입출력 예제
개념
공백으로 구분된 문자열에서 최솟값과 최댓값을 뽑아 출력하는 문제다. 공백을 만날 때마다 벡터에 문자열을 정수로 바꾼 후 값을 넣어주는 방식을 사용하면 된다.
풀이
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
string solution(string s)
{
string tmp;
vector<int> v;
int max, min;
for (const auto& str : s)
{
if (str != ' ')
tmp += str;
else
{
v.push_back(stoi(tmp));
tmp = "";
}
}
v.push_back(stoi(tmp));
min = *min_element(v.begin(), v.end());
max = *max_element(v.begin(), v.end());
string answer = to_string(min) + " " + to_string(max);
return answer;
}
반응형