BestCoder 2nd Anniversary 1001/hdu5718 Oracle

题目链接: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`.
 

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<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-909101010个数字分别有多少个。不难看出,最小的一个存在的数字和其余的数字降序排列的相加就是答案,但是最小的那个数字不能是 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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值