[프로그래머스] Level.2 연습문제-최댓값과 최솟값
2021. 1. 12. 21:26ㆍAlgorithm/프로그래머스
코딩테스트 연습 - 최댓값과 최솟값
문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 (최소값) (최대값)형태의 문자열을 반환하는 함수, solution을 완성하세요. 예를
programmers.co.kr
쉬운 문제임에도 불구하고 테스트케이스 예제만 생각하다가 틀렸다 ㅎㅎㅎㅎㅎ
공백을 제거하고 문자를 생각한다고 무조건 숫자가 한자리라고 예상하고 코드를 짰다...
실행시켰더니 다 빵점나와서 순간 흠칫했다...^^ 제발 문제 좀 자세히 읽자..하하
#include <string>
#include <vector>
#include <algorithm>
#include<iostream>
using namespace std;
string solution(string s) {
int idx = 0;
string answer = "";
string number = "";
vector<int> num;
while (1) {
string number = "";
while (1) {
if (idx == s.size()||s[idx]==' ') break;
number += s[idx++];
}
num.push_back(stoi(number));
if (idx == s.size()) break;
else idx++;
}
int min = *min_element(num.begin(), num.end());
int max = *max_element(num.begin(), num.end());
answer +=to_string(min);
answer += " ";
answer += to_string(max);
return answer;
}
'Algorithm > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level.2 String,Date-중성화 여부 파악하기 (0) | 2021.01.13 |
---|---|
[프로그래머스] Level.2 String/Date- 이름에 el이 들어가는 동물 찾기 (0) | 2021.01.13 |
[프로그래머스] Level.2 연습문제- 다음 큰 숫자 (0) | 2021.01.12 |
[프로그래머스] Level.2 월간코드챌린지 시즌 1-삼각 달팽이 (0) | 2021.01.12 |
[프로그래머스]Level.2 String,Date- 루시와 엘라찾기 (0) | 2021.01.12 |