加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

BestCoder 2nd Anniversary--1001Oracle

发布时间:2020-12-12 16:16:02 所属栏目:百科 来源:网络整理
导读: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,i
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,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%dn",(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("Uncertainn");
            continue;
        }
        if(k==1){
            save[len-1]='';
            printf("%sn",&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]='';
        printf("%s",save);
        printf("n");
    }
    
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读