四川省ACM竞赛(2013)---K - Kia's Calculation

来源:互联网 发布:vmware player安装mac 编辑:程序博客网 时间:2024/05/02 01:59

K - Kia's Calculation
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

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 ?
 

Input

The 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.
 

Output

For test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.
 

Sample Input

159583036
 

Sample Output

Case #1: 8984
 


AC CODE:

#include <iostream>#include <cstring>#define MAX 1000001using namespace std;char t1[MAX];char t2[MAX];void output(int a,int n){for(int i=0;i<n;i++)cout<<a;}int main(){int T;int count=0;cin>>T;while(T--){int s1[11]={0};int s2[11]={0};scanf("%s%s",t1,t2);int i,j;int len=0;for(i=0;t1[i];i++){len++;s1[ t1[i]-'0' ]++;}for(i=0;t2[i];i++){s2[ t2[i]-'0' ]++;}cout<<"Case #"<<++count<<": ";if(len==1){cout<<(t1[0]+t2[0]-'0'-'0')%10<<endl;continue;}int left=1;int right=8;int t=0;int count1,count2;for(i=1;i<10;i++){if(s1[i]==0)continue;for(j=1;j<10;j++){if(s2[j]==0)continue;if((i+j)%10>t){t=(i+j)%10;count1=i;count2=j;}}}if(t==0){cout<<0<<endl;continue;}cout<<t;s1[count1]--;s2[count2]--;int sum[11]={0};for(i=9;i>=0;i--)        // 1234 5678{for(j=0;j<10;j++){if(s2[j]==0)continue;int t=i-j;if(t<0)t+=10;if(s1[t]==0)continue;if(t==2){//cout<<s2[7]<<endl;//cout<<j<<" "<<i<<endl;}if(s1[t]>s2[j]){sum[i] += s2[j];s1[t]-=s2[j];s2[j]=0;}else{sum[i] += s1[t];s2[j]-=s1[t];s1[t]=0;}}}for(i=9;i>=0;i--){output(i,sum[i]);}cout<<endl;}return 0;}




0 0
原创粉丝点击