zoukankan      html  css  js  c++  java
  • hdu4726

    Kia's Calculation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 588    Accepted Submission(s): 158


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

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int a[30],b[30];
    char A[2000000],B[2000000];
    int numa[2000000],numb[1000000];
    int cnt[2000000];
    int main()
    {
        int t,num=1,i,len,j,x,y,min,count,k;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s%s",A,B);
            len=strlen(A);
            for(i=0; i<len; i++)
            {
                numa[i]=A[i]-'0';
                numb[i]=B[i]-'0';
            }
            if(len==1)
            {
                printf("Case #%d: %d ",num++,(numa[0]+numb[0])%10);
                continue;
            }
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            for(i=0; i<len; i++)
            {
                a[numa[i]]++;
                b[numb[i]]++;
            }
            for(k=0; k<len;)
            {
                min=-1;
                for(i=0; i<=9; i++)
                {
                    if(k==0&&i==0)
                        continue;
                    for(j=0; j<=9; j++)
                    {
                        if(k==0&&j==0)
                            continue;
                        if(a[i]&&b[j]&&(i+j)%10>min)
                        {
                            x=i;
                            y=j;
                            min=(x+y)%10;
                        }
                    }
                }
                a[x]--;
                b[y]--;
                cnt[k++]=min;
                if(k>1)
                {
                    while(a[x]>0&&b[y]>0)        //剪枝,一旦找到比较大的,可以看看是否符合下一位
                    {
                        cnt[k++]=min;
                        a[x]--;
                        b[y]--;
                    }
                }
            }
            printf("Case #%d: ",num++);
            for(i=0;i<len;i++)
            {
                if(cnt[i]!=0)
                break;
            }
            if(i==len)
            {
                printf("0 ");
                continue;
            }
            for(;i<len;i++)
            printf("%d",cnt[i]);
            printf(" ");
        }
        return 0;
    }

     
  • 相关阅读:
    test
    设置section的距离
    UISearchBar的应用
    Centos6.6 系统优化
    Linux系统之间文件传输 scp 命令
    MySQL 数据库安装
    AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件
    Ubuntu 16.04 Chrome浏览器安装flash player插件
    Centos6.6 yum源更新
    qume-kvm 命令管理
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3317630.html
Copyright © 2011-2022 走看看