题目链接: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`.
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
(1≤T≤10)
, which denotes the number of test cases.
For each test case, the single line contains an integer n (1≤n<1010000000) .
For each test case, the single line contains an integer n (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
先记录
0−90-90−9这
101010个数字分别有多少个。不难看出,最小的一个存在的数字和其余的数字降序排列的相加就是答案,但是最小的那个数字不能是
000,因为题面上说明是正整数。将这两个数加起来时,注意处理进位问题。考虑无解的情况,即一串数字中仅存在
111个非
000数字或不存在。
#include <iostream>
#include<cstdio>
#include<cstring>
#define N 11000000
using 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;
}
}