본문 바로가기

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

[프로그래머스 Level 2, C++] 최댓값과 최솟값

728x90
반응형

문제 : 최댓값과 최솟값


풀이 방법

if else 문으로 숫자일 경우를 배열에 넣고 최댓값과 최솟값만 구해주면 된다


소스 코드

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string solution(string s) {
    string answer = "";
    vector<int> arr;
    string temp;
    
    for(char & c : s) {
        if(c == '-' || c != ' ') {
            temp.push_back(c);
        }
        else if (c == ' ') {
            arr.push_back(stoi(temp));
            temp.clear();
        }
    }
    arr.push_back(stoi(temp));
    
    answer.append(to_string(*min_element(arr.begin(), arr.end())));
    answer.push_back(' ');
    answer.append(to_string(*max_element(arr.begin(), arr.end())));
    return answer;
}

 

728x90
반응형