
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
vector<int>v;
int num = 0;
for(int i = size(s)-1; i>=0; --i){
if(isdigit(s[i])) num = int(s[i])-'0';
else{
if(s[i] == '-') num *= -1;
if(s[i] == ' '){
v.push_back(num);
num = 0;
}
}
if(i==0) v.push_back(num);
}
sort(v.begin(), v.end());
string answer = to_string(v[0]);
answer += " ";
answer += to_string(v[v.size()-1]);
return answer;
}
연습문제는 다 맞는데 제출하면 다 틀렸다고 나온다
왜...?!?!?
아
두자리수 이상을 고려하지 않고 풀었다
이걸 반영하면
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
vector<int>v;
string t = "";
for(auto x : s){
if(x == ' ') {
v.push_back(stoi(t));
t="";
}
else t += x;
}
v.push_back(stoi(t));
sort(v.begin(), v.end());
string answer = to_string(v[0]) + " " + to_string(v[v.size()-1]);
return answer;
}
이렇게 바꾸면... 성공!
'C++ 문제풀기 > 백준-프로그래머스' 카테고리의 다른 글
| 프로그래머스 | N 보다 큰 수 (0) | 2025.09.13 |
|---|---|
| 프로그래머스 | 이진변환 (0) | 2025.09.13 |
| 프로그래머스 | [PCCP 기출문제] 1번 / 동영상 재생기 (0) | 2025.09.13 |
| 백준 | 2839. 설탕배달 (7) | 2025.07.02 |
| 백준 | 2178. 미로 탐색 (0) | 2025.06.27 |