콘솔창 & 윈도우창/코딩 테스트

[백준 실버 5] 1427 소트인사이드

뽀또치즈맛 2024. 10. 23. 10:39

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;
}