hdu-5718 Oracle(水题)

题目链接:

Oracle

Time Limit: 8000/4000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)


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<10^10000000).
 
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的数,使其和最大;
 
思路:
 
贪心,可知应该把大于0的最小数当做单独的那一个数,这样才能保证最大;然后就是一个大数加法;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e7+10;
const int maxn=1e3+10;
const double eps=1e-6;

char s[N];
int flag[11],ans[N];

int main()
{
        
        int t;
        read(t);
        while(t--)
        {
            mst(flag,0);
            scanf("%s",s);
            int len=strlen(s),num=0;
            For(i,0,len-1)if(s[i]=='0')num++;
            if(num==len-1)cout<<"Uncertain
";
            else 
            {
                int mmin=9;
                For(i,0,len-1)flag[s[i]-'0']++;
                For(i,1,9)
                {
                    if(flag[i]){flag[i]--,mmin=i;break;}
                }
            int cnt=0;
            for(int i=0;i<=9;i++)
                while(flag[i]--)ans[++cnt]=i;
            ans[1]+=mmin;
            ans[len]=0;
            for(int i=1;i<=len-1;i++)
            {
                ans[i+1]+=ans[i]/10;
                ans[i]=ans[i]%10;
            }
            if(ans[len])printf("%d",ans[len]);
            for(int i=len-1;i>0;i--)printf("%d",ans[i]);
            printf("
");
            }
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5680221.html