1. 문제
/*
문제 : 정수 배열 arr을 오름차순으로 변환하는 솔루션 함수 완성하기
조건 :
- arr 길이는 2이상 10의5제곱 이하
- arr 원소값은 -100,000이상 100,0000이하임
*/
2. 문제의 솔루션 구현 안 된 코드
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
vector<int> solution(vector<int> arr)
{
/*여길 채우기*/
return arr;
}
//-------------------------------------------------------------------------
void print(vector<int> vec)
{
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
//-------------------------------------------------------------------------
int main()
{
print(solution({ 1, -5, 2, 4, 3 })); // -5 1 2 3 4
print(solution({ 2, 1, 1, 3, 2, 5, 4 })); // 1 1 2 2 3 4 5
print(solution({ 6, 1, 7 })); // 1 6 7
return 0;
}
3. 정답
#include <vector>
#include <algorithm> //sort를 위해 선언
#include <iterator>
#include <iostream>
using namespace std;
vector<int> solution(vector<int> arr)
{
sort(arr.begin(), arr.end());
return arr;
}
//-------------------------------------------------------------------------
void print(vector<int> vec)
{
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
//-------------------------------------------------------------------------
int main()
{
print(solution({ 1, -5, 2, 4, 3 })); // -5 1 2 3 4
print(solution({ 2, 1, 1, 3, 2, 5, 4 })); // 1 1 2 2 3 4 5
print(solution({ 6, 1, 7 })); // 1 6 7
return 0;
}
4. 내 코멘트..
- sort함수 첨 봤다... 10분만에 풀어야 하는데 못 풀었다
- sort함수 쓰려면 #include <algorithm>
- sort(시작, 끝)
- 난 바보다..
- 대신 이거 아니까 내림차순 정렬할 수 있게 되었다. sort(arr.rbegin(), arr.rend());
- 부끄럽다
'알고리즘(C++)' 카테고리의 다른 글
배열 - 두개 뽑아서 더해서 정렬하기 (0) | 2025.01.30 |
---|---|
배열 - 배열제어하기 (0) | 2025.01.30 |
큐- 요세푸스 문제 (0) | 2025.01.28 |
스택 - 10진수를 2진수로 바꾸기 (0) | 2025.01.28 |
스택 - 괄호 짝 맞추기 (0) | 2025.01.27 |