이 문제에서 큰 수 N이 주어집니다. 우리의 임무는 주어진 수의 가장 작은 순열을 찾는 것입니다.
문제를 이해하기 위해 예를 들어보겠습니다.
입력
N = 4529016
출력
1024569
솔루션 접근 방식
문제에 대한 간단한 해결책은 긴 정수 값을 string형에 저장하는 것입니다. 그런 다음 결과인 문자열을 정렬합니다. 그러나 선행 0이 있는 경우 0이 아닌 첫 번째 값 이후로 이동합니다.
우리 솔루션의 작동을 설명하는 프로그램
예시
#include <bits/stdc++.h>
using namespace std;
string smallestNumPer(string s) {
int len = s.length();
sort(s.begin(), s.end());
int i = 0;
while (s[i] == '0')
i++;
swap(s[0], s[i]);
return s;
}
int main() {
string s = "4529016";
cout<<"The number is "<<s<<endl;
cout<<"The smallest permutation of the number is "<<smallestNumPer(s);
return 0;
} 출력
The number is 4529016 The smallest permutation of the number is 1024569