[코테/C++] Level2 - 최댓값과 최솟값

cherry_·2023년 9월 30일
0

코딩테스트 준비

목록 보기
1/15

최댓값과 최솟값

문제

풀이

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

using namespace std;

string solution(string s) {
    string answer = "";
    vector<int> v;
    string tmp;
    
    for(int i=0; i<s.length(); i++){
        if(s[i] != ' '){
            tmp += s[i];        
        }
        else{
            v.push_back(stoi(tmp));
            tmp.clear();
        }
    }
    v.push_back(stoi(tmp));
    sort(v.begin(), v.end());
    
    answer = to_string(v.front()) + " " + to_string(v.back());
    return answer;
}

//s를 돌면서 i가 공백이 아니면 tmp에 저장 -> 벡터에 추가
//벡터 정렬 -> 첫째가 최소, 마지막이 최대

기억할 함수

  • string to int : stoi()
  • int to string : to_string()

0개의 댓글