﻿HDU 5718 Oracle(贪心)-Ocrosoft

# Oracle

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

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 (1≤T≤10), which denotes the number of test cases.
For each test case, the single line contains an integer (1≤n<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.



Solution

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 10000000+10
using namespace std;
int f[10],a[MAXN];
int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
ms(f),ms(a);
char c;
while(isdigit(c=getchar()))f[c-'0']++;
int sum=0;
for(int i=1;i<=9;i++)sum+=f[i];//计算除0外的数字个数
if(sum<2)printf("Uncertain\n");
else
{
int t=0,k=0;
for(int i=1;i<=9;i++)
if(f[i]){f[i]--;k=i;break;}//寻找1位的数
for(int i=0;i<=9;i++)
while(f[i]--)a[t++]=i;//构造len-1位的数
for(int i=0;i<t;i++)
{
int kk=a[i];
a[i]=(a[i]+k)%10;
k=(k+kk)/10;
}
if(k)a[t++]=k;
while(t--)printf("%d",a[t]);
printf("\n");
}
}
return 0;
}