알고리즘 문제풀이/백준
[백준 1427] 소트인사이드
홍시홍
2019. 12. 19. 22:16
https://www.acmicpc.net/problem/1427
1427번: 소트인사이드
첫째 줄에 정렬하고자하는 수 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.
www.acmicpc.net
요구사항
1. 주어진 수 정렬
풀이
1. str로 수가 주어진다
2. stoi로 int형으로 바꾼다
3. %연산을 이용해서 한자리씩 map에 저장후 퀵정렬 이용
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
typedef long long ll;
int map[11];
void Quicksort(int l, int r, int arr[])
{
if (l >= r)
return;
int i = l;
int j = r;
int pivot = arr[(l + r) / 2];
while (i <= j)
{
while (pivot < arr[i])
i++;
while (pivot > arr[j])
j--;
if (i <= j)
{
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if(i<r)
Quicksort(i, r, arr);
if (j > l)
Quicksort(l, j, arr);
}
int main()
{
ios_base::sync_with_stdio(false);
string str;
ll n;
cin >> str;
n = stoi(str);
for (int i = 0; i < str.size(); i++)
{
int nown = n % 10;
map[i] = nown;
n = n / 10;
}
Quicksort(0, str.size(), map);
for (int i = 0; i < str.size(); i++)
cout << map[i];
}