# Kia's Calculation（贪心）

http://acm.hdu.edu.cn/showproblem.php?pid=4726

`#include <stdio.h>#include <iostream>#include <map>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-8#define PI acos(-1.0)using namespace std;const int maxn = 1000010;int fir[12],sec[12];char s1[maxn],s2[maxn],ans[maxn];int main(){int test,len;scanf("%d",&test);for(int item = 1; item <= test; item++){scanf("%s %s",s1,s2);len = strlen(s1);memset(fir,0,sizeof(fir));memset(sec,0,sizeof(sec));if(strcmp(s1,"0") == 0){printf("Case #%d: ",item);printf("%s\n",s2);continue;}if(strcmp(s2,"0") == 0){printf("Case #%d: ",item);printf("%s\n",s1);continue;}for(int i = 0; i < len; i++){fir[s1[i]-'0']++;sec[s2[i]-'0']++;}int cnt = 0;int Max = -1;int a,b;//找第一个数字，不能找前导零for(int i = 1; i <= 9; i++){if(fir[i] == 0)continue;for(int j = 1; j <= 9; j++){if(sec[j] == 0) continue;int t = (i+j)%10;if(t > Max){Max = t;a = i;b = j;}}}ans[++cnt] = Max + '0';fir[a]--;sec[b]--;while(cnt < len){Max = -1;for(int i = 0; i <= 9; i++){if(fir[i] == 0) continue;for(int j = 0; j <= 9; j++){if(sec[j] == 0) continue;int t = (i+j)%10;if(t > Max){Max = t;a = i;b = j;}}}ans[++cnt] = Max + '0';fir[a]--;sec[b]--;}printf("Case #%d: ",item);int i;for(i = 1; i <= len; i++)if(ans[i] != '0')break;//如果是0，直接输出0if(i == len+1)printf("0\n");else{for( ;i <= len; i++)printf("%c",ans[i]);printf("\n");}}return 0;}`

0 0