BestCoder 2nd Anniversary--1001Oracle

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


考虑到数字特别大,用char数组保存下来。wa点是要将数字分成两个正整数,所以0不算,而且只有一个正整数其他都是0的数也是uncertain。

先排序,然后找正整数放到末尾。

下面是代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>
#include <cmath>
using namespace std;
char save[10000005];
bool compare(int a,int b)
{
    return a>b;   //升序排列,如果改为return a>b,则为降序
    
}
int main(){
    unsigned n,i,k,judge;
    scanf("%d",&n);
    unsigned long len;
    while(n--){
        memset(save, 0, sizeof(save));
        scanf("%s",save);
        len=strlen(save);
        //cout<<"len :"<<len<<endl;
        if(len==2&&(save[0]+save[1]-'0'-'0'>9)&&save[0]!='0'&&save[1]!='0'){
            printf("1%d\n",(save[0]+save[1]-'0'-'0')%10);
            
            continue;
        }
        sort(save,save+len,compare);
        for(i=0;i<len;i++){
            if(save[i]!='0'){
                k=i;
            }else{
                break;
            }
        }
        //cout<<"k :"<<k<<endl;
        if(len==1||k==0){
            printf("Uncertain\n");
            continue;
        }
        if(k==1){
            save[len-1]='\0';
            printf("%s\n",&save);
            continue;
        }
        k++;
        //cout<<save<<endl;
        if(k!=len){
            char t=save[k-1];
            for(i=k-1;i<len-2;i++){
                save[i]='0';
            }
            save[len-2]=t;
        }else{if((save[len-1]-'0')+(save[len-2]-'0')>=10){
            save[len-2]=(((save[len-1]-'0')+(save[len-2]-'0'))%10)+'0';
            if(save[len-3]+1<='9'){
                save[len-3]=save[len-3]+1;
            }else{
                k=3;
                while(save[len-k]+1>='9'){
                    if(k==len){
                        save[len-k]='0';
                        break;
                    }
                    save[len-k-1]=save[len-k-1]+1;
                    save[len-k]='0';
                    k++;
                }
                printf("1");
            }
        }else{
            save[len-2]=(save[len-1]-'0'+save[len-2]);
        }
        }
        save[len-1]='\0';
        printf("%s",save);
        printf("\n");
    }
    
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值