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 ? InputThe rst 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 10 6, and without leading zeros.OutputFor test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.Sample Input
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 ? InputThe rst 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 10 6, and without leading zeros.OutputFor test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.Sample Input
1 5958 3036Sample Output
Case #1: 8984
思路:先是统计0~9之间的个数,然后从9开始,第一种是:两项之和小于10;第二种是:两项之和大于9,r=j+10-p,判断 r是否存在; 在这里有一个bug就是strlen(A)不能直接在循环里中使用,要用 n=strlen(A);
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[10],b[10];
char A[1000005],B[1000005];
char sum[1000005];
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%s%s",A,B);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int T=strlen(A);
for(int j=0; j<T; j++)
{
a[A[j]-'0']++;
b[B[j]-'0']++;
}
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[10],b[10];
char A[1000005],B[1000005];
char sum[1000005];
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%s%s",A,B);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int T=strlen(A);
for(int j=0; j<T; j++)
{
a[A[j]-'0']++;
b[B[j]-'0']++;
}
if(strlen(A)==1)
{
printf("Case #%d: %d\n",i,((A[0]-'0')+(B[0]-'0'))%10);
continue;
}
int max1=-1,w,e;
for(int j=1; j<10; j++)
for(int q=1; q<10; q++)
{
if(a[j]&&b[q]&&(j+q)%10>max1)
{
max1=(j+q)%10;
w=j;
e=q;
}
}
sum[0]=max1+'0';
a[w]--;b[e]--;
int m=1;
for(int j=9;j>=0;j--)
{
for(int q=0;q<10;q++)
{
if(q<=j)
{
int aw=b[q]<=a[j-q]? b[q]:a[j-q];
b[q]-=aw;a[j-q]-=aw;
while(aw--)
sum[m++]=j+'0';
}
else
{
int r=j+10-q;
int aw=b[r]<a[q]? b[r]:a[q];
b[r]-=aw;a[q]-=aw;
while(aw--)
sum[m++]=j+'0';
}
}
}
sum[m]='\0';
if(sum[0]=='0')
printf("Case #%d: 0\n",i);
else
printf("Case #%d: %s\n",i,sum);
}
return 0;
}
{
printf("Case #%d: %d\n",i,((A[0]-'0')+(B[0]-'0'))%10);
continue;
}
int max1=-1,w,e;
for(int j=1; j<10; j++)
for(int q=1; q<10; q++)
{
if(a[j]&&b[q]&&(j+q)%10>max1)
{
max1=(j+q)%10;
w=j;
e=q;
}
}
sum[0]=max1+'0';
a[w]--;b[e]--;
int m=1;
for(int j=9;j>=0;j--)
{
for(int q=0;q<10;q++)
{
if(q<=j)
{
int aw=b[q]<=a[j-q]? b[q]:a[j-q];
b[q]-=aw;a[j-q]-=aw;
while(aw--)
sum[m++]=j+'0';
}
else
{
int r=j+10-q;
int aw=b[r]<a[q]? b[r]:a[q];
b[r]-=aw;a[q]-=aw;
while(aw--)
sum[m++]=j+'0';
}
}
}
sum[m]='\0';
if(sum[0]=='0')
printf("Case #%d: 0\n",i);
else
printf("Case #%d: %s\n",i,sum);
}
return 0;
}