您的位置:首页 > 数据库 > Oracle

HDU 5718 Oracle

2016-07-17 23:35 375 查看

Oracle

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


[align=left]Problem Description[/align]
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`.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, print a positive integer or a string `Uncertain`.

[align=left]Sample Input[/align]

3
112
233
1

[align=left]Sample Output[/align]

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.

贪心策略 模拟一下就好了。把大的放在前面,记录一下最小的那个非0数字的位置然后把这个最小的非0的数字分割出来。与剩下的 相加就是最大的...

附上我愚蠢的代码

/* ***********************************************
Author        :guanjun
Created Time  :2016/7/17 18:53:46
File Name     :bc2nd_a.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;char s[10000010];
int a[10000010];
vector<int>v;
int main()
{
int t;
cin>>t;
while(t--){
scanf("%s",s);
v.clear();
cle(a);
int n=strlen(s);
if(n==1)puts("Uncertain");
else{
sort(s,s+n);
int num=0;int b,x;
int mark=0;
for(int i=0;i<n;i++){
a[i]=s[i]-'0';
if(a[i]>0)num++;
if(a[i]>0&&!mark){
b=a[i];mark=1;
x=i;
}
}
if(num==1){
puts("Uncertain");continue;
}
if(x!=0)a[0]+=b;
else a[1]+=b;
for(int i=0;i<n;i++){
if(i==x)continue;
if(a[i]>=10){
a[i]=a[i]%10;
if((i+1)!=x)a[i+1]++;
else a[i+2]++;
}
v.push_back(a[i]);
}
if(a
!=0)v.push_back(a
);
for(int i=v.size()-1;i>=0;i--){
printf("%d",v[i]);
}
puts("");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: