給兩個數,進行不進爲加法(比如9+9=8),可以調換每個數中數字的位置,但不能有前導零,求A+B得最大值。
題目要求不能有前導零,那麼我們就先找出來一個不使用0可以得到的最大的數,把他輸出後剩下的就可以正常求了,從9到0枚舉每個數,然後根據給的兩個數中每個數出現的次數,求出可以構造出幾個i,從大到小掃一遍即可。
/*=============================================================================
# Author:Erich
# FileName:
=============================================================================*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#define lson id<<1,l,m
#define rson id<<1|1,m+1,r
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1ll<<60;
const double PI=acos(-1.0);
int n,m;
int a[11],b[11];
char s[1000010];
int num[15];
int main()
{
//freopen("in.txt","r",stdin);
int tt;
scanf("%d",&tt);
int l;
int cnt=0;
while(tt--)
{
printf("Case #%d: ",++cnt);
memset(a,0,sizeof a);
memset(b,0,sizeof b);
scanf("%s",s);
l=strlen(s);
for (int i=0; i<l; i++)
{
a[s[i]-'0']++;
}
scanf("%s",s);
for (int i=0; i<l; i++)
{
b[s[i]-'0']++;
}
memset(num,0,sizeof num);
bool has=false;
for (int i=9; i>=1; i--)
{
for (int j=1; j<=9; j++)
{
int k=((i-j)+10)%10;
if (k==0) continue;
m=min(a[j],b[k]);
if (m)
{
m=1;
a[j]--; b[k]--;
printf("%d",i);
has=true; break;
}
if (has) break;
}
if (has) break;
}
if (!has)
{
printf("0");
}
else
{
for (int i=9; i>=0; i--)
{
for (int j=0; j<=9; j++)
{
int k=((i-j)+10)%10;
m=min(a[j],b[k]);
if (m)
{
a[j]-=m;
b[k]-=m;
while(m--) printf("%d",i);
}
}
}
}
puts("");
}
return 0;
}