传送门
题意:给出两个数a,b将a的每一位打乱重组成一个比b小最大的数。
思路:从小到大对a排序,选取这样的数的每一位,设置一个中间字符串t,把当前位和后面更大的位交换,若交换后,任然满足条件,则让a也执行同样的交换,否则,a不执行任何操作。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
long long b;
string a;
cin >> a >> b;
sort(a.begin(), a.end());
for (int i = 0; i<a.size(); i++) {
for (int j = i + 1; j<a.size(); j++) {
string t = a; swap(t[i], t[j]);
sort(t.begin() + i + 1, t.end());
if (stoll(t)>stoll(a) && stoll(t) <= b) swap(a[i], a[j]);
}
}
cout << a << endl;
//system("pause");
return 0;
}