BestCoder 2nd Anniversary 1001/hdu5718 Oracle

来源:互联网 发布:域名后缀cc 编辑:程序博客网 时间:2024/04/30 16:12
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5718
题目:
Problem Description
There is once a king and queen, rulers of an unnamed city, who have three daughters of conspicuous beauty.

The youngest and most beautiful is Psyche, whose admirers, neglecting the proper worship of the love goddess Venus, instead pray and make offerings to her. Her father, the king, is desperate to know about her destiny, so he comes to the Delphi Temple to ask for an oracle.

The oracle is an integer n without leading zeroes.

To get the meaning, he needs to rearrange the digits and split the number into <b>two positive integers without leading zeroes</b>, and their sum should be as large as possible.

Help him to work out the maximum sum. It might be impossible to do that. If so, print `Uncertain`.
 

Input
The first line of the input contains an integer T(1T10), which denotes the number of test cases.

For each test case, the single line contains an integer n(1n<1010000000).
 

Output
For each test case, print a positive integer or a string `Uncertain`.
 

Sample Input
31122331
 

Sample Output
2235Uncertain


先记录 0−90-909101010个数字分别有多少个。不难看出,最小的一个存在的数字和其余的数字降序排列的相加就是答案,但是最小的那个数字不能是000,因为题面上说明是正整数。将这两个数加起来时,注意处理进位问题。考虑无解的情况,即一串数字中仅存在111个非000数字或不存在。

#include <iostream>#include<cstdio>#include<cstring>#define N 11000000using namespace std;int a[11],b[N];char s[N];int main(){    int T;    cin>>T;    while(T--)    {        scanf("%s",s);        int n=strlen(s);        memset(a,0,sizeof(a));        for(int i=0;i<n;i++)    a[s[i]-'0']++;        int tp;        int flag=0;        for(int i=1;i<10;i++)            if(a[i])            {                flag+=a[i];                if(flag>=2)                    break;            }        if(flag<2)        {            cout<<"Uncertain"<<endl;            continue;        }        for(int i=1;i<10;i++)            if(a[i])            {                a[i]--;                tp=i;                break;            }        int m=0;        memset(b,0,sizeof(b));        for(int i=0;i<10;i++)            for(int j=1;j<=a[i];j++)                b[m++]=i;        for(int i=0;i<=m&&tp;i++)        {            int t=b[i]+tp;            tp=t/10;            b[i]=t%10;        }        if(b[m])    printf("%d",b[m]);        for(int i=m-1;i>=0;i--) cout<<b[i];        cout<<endl;    }}

0 0
原创粉丝点击