Kia's Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 83 Accepted Submission(s): 16
Problem 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 ?
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 106, and without leading zeros.
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.
Output
For test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.
Sample Input
1
5958
3036
Sample Output
Case #1: 8984
Source
Recommend
zhuyuanchen520
想了很久,
最后其实就是贪心构造。
最高位特殊处理。
然后后面就是不断尽量构造和大的
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-9-11 12:30:33 4 File Name :2013-9-111011.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 21 int a[20]; 22 int b[20]; 23 24 char A[2000020],B[2000020]; 25 int num1[2000020],num2[2000020]; 26 int ans[2000020]; 27 28 29 int main() 30 { 31 //freopen("in.txt","r",stdin); 32 //freopen("out.txt","w",stdout); 33 int T; 34 int iCase = 0; 35 scanf("%d",&T); 36 while(T--) 37 { 38 iCase++; 39 scanf("%s%s",A,B); 40 int n = strlen(A); 41 for(int i = 0;i < n;i++) 42 { 43 num1[i] = A[i] - '0'; 44 num2[i] = B[i] - '0'; 45 } 46 if(n == 1) 47 { 48 printf("Case #%d: %d ",iCase,(num1[0]+num2[0])%10); 49 continue; 50 } 51 memset(a,0,sizeof(a)); 52 memset(b,0,sizeof(b)); 53 for(int i = 0;i < n;i++) 54 { 55 a[num1[i]] ++; 56 b[num2[i]] ++; 57 } 58 int x = 0, y = 0; 59 int ttt = -1; 60 for(int i = 1;i <= 9;i++) 61 for(int j = 1;j <= 9;j++) 62 if(a[i] && b[j] && ((i+j)%10) > ttt ) 63 { 64 x = i; 65 y = j; 66 ttt = (x+y)%10; 67 } 68 a[x]--; 69 b[y]--; 70 int cnt = 0; 71 ans[cnt++] = (x+y)%10; 72 73 for(int p = 9;p >= 0;p--) 74 { 75 for(int i = 0;i <= 9;i++) 76 if(a[i]) 77 { 78 if(i <= p) 79 { 80 int j = p-i; 81 int k = min(a[i],b[j]); 82 a[i] -= k; 83 b[j] -= k; 84 while(k--) 85 ans[cnt++] = p; 86 } 87 int j = 10 + p - i; 88 if(j > 9)continue; 89 int k = min(a[i],b[j]); 90 a[i] -= k; 91 b[j] -= k; 92 while(k--) 93 ans[cnt++] = p; 94 } 95 } 96 printf("Case #%d: ",iCase); 97 int s = 0; 98 while(s < cnt-1 && ans[s] == 0)s++; 99 for(int i = s;i < cnt;i++) 100 printf("%d",ans[i]); 101 printf(" "); 102 } 103 return 0; 104 }