Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9 . For example, when she calculates 4567+5789 , she will get 9246 , and for 1234+9876 , she will get 0 . Ghee is angry about this, and makes a hard problem for her to solve:
Now Kia has two integers A and B , she can shuffle the digits in each number as she like, but leading zeros are not allowed. That is to say, for A=11024 , she can rearrange the number as 10124 , or 41102 , or many other, but 02411 is not allowed.
After she shuffles
A
and
B
, she will add them together, in her own way. And what will be the maximum possible sum of
A
+
B
?
The first line has a number T ( T≤25 ) , indicating the number of test cases.
For each test case there are two lines. First line has the number A , and the second line has the number B .
Both A and B will have same number of digits, which is no larger than 106 , and without leading zeros.
For test case
X
, output Case #X:
first, then output the maximum possible sum without leading zeros.
1
5958
3036
Case #1: 8984
ps:一肚子怒气啊,15wa,1ac,实在太菜了
题目大意:有两个数字,长度不大于1000000,每个数字的每位上的数字的位置可以任意改变,将两个数相加,加法规则是:每位上的数字相加%10,注意,输入的数和得到的和不能有前导0,求相加后和最大的值
基本思路:将每个数字是有哪两个数字的组合找出来,然后查询,每次都从9开始,看看能不有两个数字将其组合,一直到0,注意的是第一个数不能为0
看代码吧:
(代码有点奇葩,太长了,可以开三维数组缩短很多)
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
char A[6666666];
char B[6666666];
char c[6666666];
char d[6666666];
int a[12],b[12];
int ninex[]= {9,8,7,6,5};
int niney[]= {0,1,2,3,4};
int lnine=5;
int eightx[]= {8,9,7,6,5,4};
int eighty[]= {0,9,1,2,3,4};
int leight=6;
int sevenx[]= {7,9,6,5,4};
int seveny[]= {0,8,1,2,3};
int lseven=5;
int sixx[]= {6,8,9,5,4,3};
int sixy[]= {0,8,7,1,2,3};
int lsix=6;
int fivex[]= {5,8,9,4,3};
int fivey[]= {0,7,6,1,2};
int lfive=5;
int forex[]= {4,7,8,9,3,2};
int forey[]= {0,7,6,5,1,2};
int lfore=6;
int threex[]= {3,7,8,9,2};
int threey[]= {0,6,5,4,1};
int lthree=5;
int twox[]= {2,6,7,8,9,1};
int twoy[]= {0,6,5,4,3,1};
int ltwo=6;
int onex[]= {1,6,7,8,9};
int oney[]= {0,5,4,3,2};
int lone=5;
int zorex[]= {0,5,6,7,8,9};
int zorey[]= {0,5,4,3,2,1};
int lzore=6;
int main()
{
int t;
int m=1;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%s%s",A,B);
int la,lb;
la=strlen(A);
lb=strlen(B);
if(la==1)///判断一位数时
{
int aa=A[0]-'0';
int bb=B[0]-'0';
c[0]=(aa+bb)%10+'0';
c[la]='\0';
printf("Case #%d: %s\n",m++,c);
continue;
}
///充计所有的数字
for(int i=0; i<la; i++)
{
a[A[i]-'0']++;
}
for(int j=0; j<lb; j++)
{
b[B[j]-'0']++;
}
int f;
///将c清空,必须
memset(c,0,sizeof(c));
for(int i=0; i<la; i++)
{
///i代表数字位置
if(i==0)
{
f=0;
int j;
for(j=9; j>0; j--)
{
///j代表可以放的数字,从最大的9开始
if(j==9)
{
///当i==0时,k从1开始
for(int k=1; k<lnine; k++)
{
if(a[ninex[k]]!=0&&b[niney[k]]!=0)
{
a[ninex[k]]--;
b[niney[k]]--;
f=1;
break;
}
else if(a[niney[k]]!=0&&b[ninex[k]]!=0)
{
a[niney[k]]--;
b[ninex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==8)
{
for(int k=1; k<leight; k++)
{
if(a[eightx[k]]!=0&&b[eighty[k]]!=0)
{
a[eightx[k]]--;
b[eighty[k]]--;
f=1;
break;
}
else if(a[eighty[k]]!=0&&b[eightx[k]]!=0)
{
a[eighty[k]]--;
b[eightx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==7)
{
for(int k=1; k<lseven; k++)
{
if(a[sevenx[k]]!=0&&b[seveny[k]]!=0)
{
a[sevenx[k]]--;
b[seveny[k]]--;
f=1;
break;
}
else if(a[seveny[k]]!=0&&b[sevenx[k]]!=0)
{
a[seveny[k]]--;
b[sevenx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==6)
{
for(int k=1; k<lsix; k++)
{
if(a[sixx[k]]!=0&&b[sixy[k]]!=0)
{
a[sixx[k]]--;
b[sixy[k]]--;
f=1;
break;
}
else if(a[sixy[k]]!=0&&b[sixx[k]]!=0)
{
a[sixy[k]]--;
b[sixx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==5)
{
for(int k=1; k<lfive; k++)
{
if(a[fivex[k]]!=0&&b[fivey[k]]!=0)
{
a[fivex[k]]--;
b[fivey[k]]--;
f=1;
break;
}
else if(a[fivey[k]]!=0&&b[fivex[k]]!=0)
{
a[fivey[k]]--;
b[fivex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==4)
{
for(int k=1; k<lfore; k++)
{
if(a[forex[k]]!=0&&b[forey[k]]!=0)
{
a[forex[k]]--;
b[forey[k]]--;
f=1;
break;
}
else if(a[forey[k]]!=0&&b[forex[k]]!=0)
{
a[forey[k]]--;
b[forex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==3)
{
for(int k=1; k<lthree; k++)
{
if(a[threex[k]]!=0&&b[threey[k]]!=0)
{
a[threex[k]]--;
b[threey[k]]--;
f=1;
break;
}
else if(a[threey[k]]!=0&&b[threex[k]]!=0)
{
a[threey[k]]--;
b[threex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==2)
{
for(int k=1; k<ltwo; k++)
{
if(a[twox[k]]!=0&&b[twoy[k]]!=0)
{
a[twox[k]]--;
b[twoy[k]]--;
f=1;
break;
}
else if(a[twoy[k]]!=0&&b[twox[k]]!=0)
{
a[twoy[k]]--;
b[twox[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==1)
{
for(int k=1; k<lone; k++)
{
if(a[onex[k]]!=0&&b[oney[k]]!=0)
{
a[onex[k]]--;
b[oney[k]]--;
f=1;
break;
}
else if(a[oney[k]]!=0&&b[onex[k]]!=0)
{
a[oney[k]]--;
b[onex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==0)
{
for(int k=1; k<lzore; k++)
{
if(a[zorex[k]]!=0&&b[zorey[k]]!=0)
{
a[zorex[k]]--;
b[zorey[k]]--;
f=1;
break;
}
else if(a[zorey[k]]!=0&&b[zorex[k]]!=0)
{
a[zorey[k]]--;
b[zorex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
//cout<<j<<" "<<c[0]<<endl;
}
///这里很重要,判断当第一位都要放0的时候说明和就是0,直接跳出讯很输出
if(j==0)
{
c[i]='0';
break;
}
}
else
{
f=0;
int j;
for( j=9; j>=0; j--)
{
if(j==9)
{
for(int k=0; k<lnine; k++)
{
if(a[ninex[k]]!=0&&b[niney[k]]!=0)
{
a[ninex[k]]--;
b[niney[k]]--;
f=1;
break;
}
else if(a[niney[k]]!=0&&b[ninex[k]]!=0)
{
a[niney[k]]--;
b[ninex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==8)
{
for(int k=0; k<leight; k++)
{
if(a[eightx[k]]!=0&&b[eighty[k]]!=0)
{
a[eightx[k]]--;
b[eighty[k]]--;
f=1;
break;
}
else if(a[eighty[k]]!=0&&b[eightx[k]]!=0)
{
a[eighty[k]]--;
b[eightx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==7)
{
for(int k=0; k<lseven; k++)
{
if(a[sevenx[k]]!=0&&b[seveny[k]]!=0)
{
a[sevenx[k]]--;
b[seveny[k]]--;
f=1;
break;
}
else if(a[seveny[k]]!=0&&b[sevenx[k]]!=0)
{
a[seveny[k]]--;
b[sevenx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==6)
{
for(int k=0; k<lsix; k++)
{
if(a[sixx[k]]!=0&&b[sixy[k]]!=0)
{
a[sixx[k]]--;
b[sixy[k]]--;
f=1;
break;
}
else if(a[sixy[k]]!=0&&b[sixx[k]]!=0)
{
a[sixy[k]]--;
b[sixx[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==5)
{
for(int k=0; k<lfive; k++)
{
if(a[fivex[k]]!=0&&b[fivey[k]]!=0)
{
a[fivex[k]]--;
b[fivey[k]]--;
f=1;
break;
}
else if(a[fivey[k]]!=0&&b[fivex[k]]!=0)
{
a[fivey[k]]--;
b[fivex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==4)
{
for(int k=0; k<lfore; k++)
{
if(a[forex[k]]!=0&&b[forey[k]]!=0)
{
a[forex[k]]--;
b[forey[k]]--;
f=1;
break;
}
else if(a[forey[k]]!=0&&b[forex[k]]!=0)
{
a[forey[k]]--;
b[forex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==3)
{
for(int k=0; k<lthree; k++)
{
if(a[threex[k]]!=0&&b[threey[k]]!=0)
{
a[threex[k]]--;
b[threey[k]]--;
f=1;
break;
}
else if(a[threey[k]]!=0&&b[threex[k]]!=0)
{
a[threey[k]]--;
b[threex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==2)
{
for(int k=0; k<ltwo; k++)
{
if(a[twox[k]]!=0&&b[twoy[k]]!=0)
{
a[twox[k]]--;
b[twoy[k]]--;
f=1;
break;
}
else if(a[twoy[k]]!=0&&b[twox[k]]!=0)
{
a[twoy[k]]--;
b[twox[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==1)
{
for(int k=0; k<lone; k++)
{
if(a[onex[k]]!=0&&b[oney[k]]!=0)
{
a[onex[k]]--;
b[oney[k]]--;
f=1;
break;
}
else if(a[oney[k]]!=0&&b[onex[k]]!=0)
{
a[oney[k]]--;
b[onex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
if(j==0)///不是第一位可以为0
{
for(int k=0; k<lzore; k++)
{
if(a[zorex[k]]!=0&&b[zorey[k]]!=0)
{
a[zorex[k]]--;
b[zorey[k]]--;
f=1;
break;
}
else if(a[zorey[k]]!=0&&b[zorex[k]]!=0)
{
a[zorey[k]]--;
b[zorex[k]]--;
f=1;
break;
}
}
if(f)
{
c[i]=j+'0';
break;
}
}
}
}
}
c[la]='\0';
printf("Case #%d: %s\n",m++,c);
}
return 0;
}