hdu-5718 Oracle(水题)
阅读原文时间:2023年07月15日阅读:1

题目链接:

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 two positive integers without leading zeroes, 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<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
#include
#include
#include
#include

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 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 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('\n');
}

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\\n";  
        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("\\n");  
        }  
    }  
    return 0;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章