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`.
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
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
(1≤T≤10)
,which denotes the number of test cases.
For each test case,the single line contains an integer
n
(1≤n<1010000000)
.
For each test case,the single line contains an integer
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%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; }原文链接:https://www.f2er.com/oracle/213541.html