https://www.acmicpc.net/problem/1427
string으로 받으면 cin.eof()로 안받아도 된다.
띄어쓰기도 없는 걸 보니깐 string으로 받는 게 편한 거 같다.
여기서 내림차순으로 수를 정렬을 하면 되는데
사실 vector의 sort는 퀵소트라 빠르긴 하다.
하지만, 직접 선택 정렬을 통해 구현할 수도 있다.
현재 범위에서 maxIdx를 찾고,
최대값을 앞으로 보내는 식으로 정렬한다.
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string str;
vector<int> v;
cin >> str;
for (int i = 0; i < str.size(); i++) {
v.push_back(stoi(str.substr(i, 1)));
}
for (int i = 0; i < str.size(); i++) {
int maxIdx = i;
for (int j = i + 1; j < str.size(); j++) {
if (v[j] > v[maxIdx]) {
maxIdx = j;
}
}
if (v[i] < v[maxIdx]) {
int temp = v[i];
v[i] = v[maxIdx];
v[maxIdx] = temp;
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i];
}
return 0;
}
'콘솔창 & 윈도우창 > 코딩 테스트' 카테고리의 다른 글
프로그래머스 LV.2 멀리 뛰기 (0) | 2024.10.28 |
---|---|
[백준 골드5] 2023 신기한 소수 (0) | 2024.10.27 |
[백준 골드2] 1377 버블 소트 (1) | 2024.10.18 |
[실버3] 1021 회전하는 큐 (0) | 2024.10.18 |
[실버3] 17253 삼삼한 수 2 (0) | 2024.10.18 |