HUST信心大涨迎省赛之《我要冲银牌》K——字符串——Kia's Calculation

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

1 5958 3036
 

Sample Output

Case #1: 8984
 大意:先判第一个数,再判后面的,从i=9开始输出符合的多少的组数
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAX = 1000;

int t1[MAX][MAX],t2[MAX][MAX];

int x[MAX],y[MAX];

int main()

{

    //freopen ("K.in", "r", stdin);

    int T;

    scanf("%d",&T);

    int xy = 1;

    while(T--){

        memset(t1,0,sizeof(t1));

        memset(t2,0,sizeof(t2));

        int n,m;

        scanf("%d%d",&n,&m);

        int temp1 = 1;

       while(n){

          x[temp1++] = n%10;

          n/=10;

       }

       int temp2 = 1;

       while(m){

         y[temp2++] = m%10;

         m/=10;

       }

       sort(x+1,x+temp1);

       sort(y+1,y+temp2);

       int x0 = 1;

       temp1 --;

       temp2 --;

       do{

           for(int i = 1; i <=  temp1;i++){

               t1[x0][i] = x[i];

           }

           x0++;

       }

       while(next_permutation(x+1,x+temp1+1));

        int y0 = 1;

        do{

        for(int i = 1; i <= temp2; i++)

              t2[y0][i] = y[i];

        y0++;

       }while(next_permutation(y+1,y+temp2+1));



       //printf ("x0: %d y0: %d\n", x0, y0);

           int sum1 = 0;

           for(int i = 1; i <= x0;i++){

            for(int j = 1; j <= y0;j++){

            int sum = 0;

              if(t1[i][1] == 0 || t2[j][1] == 0)

                  continue;

              int k = 1;

            int temp3 = temp1;

            int temp4 = temp2;

            if (temp3 > temp4)

            {

                while(temp3 > temp4){

                sum = sum*10 + t1[i][k];

                    temp4++;

                   k++;

                }

            }

            if (temp4 > temp3)

            {

                while(temp4 > temp3){

                sum = sum*10 + t2[j][k];

                  temp3++;

                  k++;

                }

            }

            if(temp1 > temp2){

           for(int m1 = k,m2 = 1; m1 <=temp3;m1++,m2++)

            sum = (t1[i][m1] + t2[j][m2])%10 + sum * 10;

            sum1 = max(sum1,sum);

            }

            else {

                for(int m1 = 1,m2 = k; m2 <= temp4; m1++,m2++)

                    sum = (t1[i][m1] + t2[j][m2])%10 + sum *10;

                sum1 = max(sum1,sum);

            }

            }

         }

        printf("Case #%d:",xy++);

        printf(" %d\n",sum1);

        }

        return 0;

 }
View Code

 

你可能感兴趣的:(字符串)