Oracle
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 79 Accepted Submission(s): 41
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 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 (1≤T, 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 n (1≤n<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
Hint
In the first example, it is optimal to split $ 112 $ into $ 21 $ and $ 1 $, and their sum is $ 21 + 1 = 22 $.
In the second example, it is optimal to split $ 233 $ into $ 2 $ and $ 33 $, and their sum is $ 2 + 33 = 35 $.
In the third example, it is impossible to split single digit $ 1 $ into two parts.
Source
题意:将一个大数分解成两个数字,要求两个数字是没有前导0的正整数,然后问相加的结果的最大值.
题解:先对输入的串从大到小排个序,如果输入的串长度为1或者 除了第一位全部都是 0,那么无解,其余的情况将第一个大于0的数取出来,然后剩下的数组成一个串相加即可。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 10000005 char str[N]; char result[N]; char b[100]; int cmp(char a,char b) { return a>b; } void reverse( char *s ) /*将字符串逆置*/ { int length; int i = 0; char temp; length = strlen( s ); while( i < length - i - 1 ) { temp = s[i]; s[i] = s[length - i - 1]; s[length - i - 1] = temp; i++; } } void AddBigNum( char* s1, char* s2, char* result ) { int len1 = strlen( s1 ); int len2 = strlen( s2 ); int acc = 0, temp, i; /*acc为进位标记*/ if( s1 == NULL || s2 == NULL || result == NULL ) { return; } reverse( s1 ); reverse( s2 ); for( i = 0; i < len1 && i < len2; i++ ) { temp = s1[i] - '0' + s2[i] - '0' + acc; /*计算每位的实际和*/ result[i] = temp % 10 + '0'; /*通过求余数来确定每位的最终值*/ if( temp >= 10 ) /*通过这个if..else..条件来判断是否有进位,并设置进位值*/ acc = 1; else acc = 0; } if( i < len1 ) /*两个加数位数不同*/ { for( ; i < len1; i++ ) { temp = s1[i] - '0' + acc; /*依旧要考虑进位,比如9999 + 1的情况*/ result[i] = temp % 10 + '0'; if( temp >= 10 ) acc = 1; else acc = 0; } } if( i < len2 ) { for( ; i < len2; i++ ) { temp = s2[i] - '0' + acc; result[i] = temp % 10 + '0'; if( temp >= 10 ) acc = 1; else acc = 0; } } if( acc == 1 ) /*考虑如:123 + 911 = 1034的情况,如果不增加这个条件会得到结果为034,进位被舍弃*/ result[i++] = '1'; result[i] = '