Programming/Baekjoon

백준 16943 c++ [숫자재배치]

fishersheep 2022. 5. 5. 16:44
반응형
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>

using namespace std;



int main()
{	
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int a, b, c;

	c = -1;

	cin >> a >> b;

	string str = to_string(a);
	sort(str.begin(), str.end());	//next_permutation을 사용하기 위해 오름차순 정렬

	do
	{
		int num = stoi(str);

		if (str[0] == '0')continue;	//문자열의 첫번째 문자가 0이면 정수로변환했을때 자리수가 변경됨으로 순열X

		if (num < b && c < num)	//b보다 작으면서 현재 c보다 크다면 num저장
			c = num;


	} while (next_permutation(str.begin(), str.end()));	//next_permutation을 활용하여 순열출력


	cout << c;	//결과출력

	return 0;
}

 

결과화면

반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 1051 c++ [숫자정사각형]  (0) 2022.05.06
백준 5567 c++ [결혼식]  (0) 2022.05.05
백준 1527 c++ [금민수의개수]  (0) 2022.05.04
백준 1063 c++ [킹]  (0) 2022.05.04
[백준] 2667 단지번호붙이기 c++  (0) 2022.05.03