728x90
반응형

programmers.co.kr/learn/courses/30/lessons/12939

 

코딩테스트 연습 - 최댓값과 최솟값

문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 (최소값) (최대값)형태의 문자열을 반환하는 함수, solution을 완성하세요. 예를

programmers.co.kr

 

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

vector<string> split(string input, char delimiter) {
    vector<string> answer;
    stringstream ss(input);
    string temp;

    while (getline(ss, temp, delimiter)) {
        answer.push_back(temp);
    }
    return answer;
}

string solution(string s) {
    string answer = "";
    vector<string> nums = split(s, ' ');
    vector<int> num;
    int max = -9874;
    int min = 9874;

    for(int i= 0; i < nums.size(); i++){
        num.push_back(atoi(nums[i].c_str()));
        if(num[i] > max)
            max = num[i];
        if(num[i] < min)
            min = num[i];
    }
    answer = to_string(min) + " " + to_string(max);
    return answer;
}
728x90
반응형

+ Recent posts