7-16 Bestcoder a Oracle

 

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 325    Accepted Submission(s): 139


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
3 112 233 1
 
Sample Output
22 35 Uncertain
Hint
In the first example, it is optimal to split $ 112 $ into $ 21 $ and $ 1 $, and their sum is $ 21 + 1 = 22 $. In the second example, it is optimal to split $ 233 $ into $ 2 $ and $ 33 $, and their sum is $ 2 + 33 = 35 $. In the third example, it is impossible to split single digit $ 1 $ into two parts.
 
Source
 
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
char num[10000005];
int ans[10000005];
int main()
{

    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        int i=0;
        int ge[10];
        for(int j=0; j<10; j++) ge[j]=0;
        scanf("%s",num);
        int len=strlen(num);
        if(len<=1)
        {
            printf("Uncertain
");
        }
        else
        {
            for(int i=0;i<len;i++){
                ge[num[i]-'0']++;
            }
            int cnt=0;
            for(int i=9;i>=0;i--){
                for(int j=0;j<ge[i];j++){
                    ans[cnt++]=i;
                }
            }
//            for(int i=0;i<cnt;i++) cout<<ans[i]<<"  ";
            int flag=1;
            if(ans[cnt-1]==0){
                flag=0;
                for(int i=cnt-2;i>0;i--){
                    if(ans[i]!=0){
                        flag=1;
                        ans[cnt-1]=ans[i];
                        ans[i]=0;
                        break;
                    }
                }
            }
            if(!flag){
                printf("Uncertain
");
                continue;
            }
            int tmp=0;
           // for(int j=0; j<len; j++) cout<<ans[j]<<endl;
            //cout<<len;
            for(int j=len-2; j>=0; j--)
            {
                ans[j]=ans[j]+tmp;
                if(j==len-2) ans[j]=ans[j]+ans[len-1];
                if(ans[j]>=10)
                {
                    ans[j]-=10;
                    tmp=1;
                }
                else
                {
                    tmp=0;
                }
            }
            if(tmp)
            {
                printf("1");
            }
            for(int i=0; i<=len-2; i++)
                printf("%d",ans[i]);
            printf("
");
        }

    }
    return 0;
}
View Code
 这道题没有什么好说到,是一道送分题。
但是注意两个相加到数都必须是正整数,所以如果ans到最后一位是0到话要和非第一位到非0数交换,如果没有非0的数,则
没法完成神谕。
原文地址:https://www.cnblogs.com/superxuezhazha/p/5680203.html