You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.2 KiB
C++
93 lines
2.2 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef long long ll;
|
|
int num[12], na[20], nb[20];
|
|
int intlen(ll x)
|
|
{
|
|
int len = 0;
|
|
while (x)
|
|
x /= 10, len++;
|
|
return len;
|
|
}
|
|
int main()
|
|
{
|
|
ll a, b;
|
|
scanf("%lld%lld", &a, &b);
|
|
ll q = a;
|
|
while (a)
|
|
{
|
|
int r = a % 10;
|
|
a /= 10;
|
|
num[r]++;
|
|
}
|
|
if (q == b)
|
|
{
|
|
printf("%lld", b);
|
|
return 0;
|
|
}
|
|
int lena = intlen(q);
|
|
int lenb = intlen(b);
|
|
if (lena < lenb)
|
|
{
|
|
for (int j = 9; j >= 0; j--)
|
|
{
|
|
while (num[j])
|
|
{
|
|
num[j]--;
|
|
printf("%d", j);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
if (lena == lenb)
|
|
{
|
|
int temb = lenb;
|
|
while (b)
|
|
{
|
|
int r = b % 10;
|
|
b /= 10;
|
|
nb[temb--] = r;
|
|
}
|
|
for (int i = 1; i <= lena; i++)
|
|
{
|
|
if (num[nb[i]])
|
|
{
|
|
num[nb[i]]--;
|
|
na[i] = nb[i];
|
|
}
|
|
else
|
|
{
|
|
bool fl = 0;
|
|
while (!fl)
|
|
{
|
|
for (int j = nb[i] - 1; j >= 0; j--)
|
|
{
|
|
if (num[j])
|
|
{
|
|
na[i] = j;
|
|
num[j]--;
|
|
for (int l = 1; l <= i; l++)
|
|
printf("%d", na[l]);
|
|
for (int kk = 9; kk >= 0; kk--)
|
|
{
|
|
while (num[kk])
|
|
{
|
|
printf("%d", kk);
|
|
num[kk]--;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
i = i - 1;
|
|
num[na[i]]++;
|
|
}
|
|
}
|
|
}
|
|
for (int i = 1; i <= lena; i++)
|
|
printf("%d", na[i]);
|
|
}
|
|
return 0;
|
|
} |