hdoj5718 Oracle (BestCoder 2nd Anniversary 1001)

题目:

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 two positive integers without leading zeroes, 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).
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.

此题就是输入一个数字,将此数字分割成2个正整数,然后求和,问最大的和是多少。
此题需注意:

  1. 分割成的两个数都是正整数(不能为0);
  2. 此题如果数字里只有一个或没有正整数,则无解;
  3. 此题的数字可能非常非常非常大,因此当然不能直接加喽,需用大数加法。

方法:
其实就是把其中最小的正整数作为一个加数加大的数字即可(排序)。

参考代码:

#include 
#include 
#include 
using namespace std;
char n[15000000];
char n1[15000000];
long long count_string(char n[],long long len) {
    long long count = 0;
    for (long long i = 0;i < len;++i) {
        if (n[i] != '0') {
            count++;
        }
    }
    return count; 
}
void result(char n[],long long len) {
    long long count = count_string(n,len);
    if (len == 1) {
        printf("Uncertain\n");
    }
    else if (count == 1 || count == 0) {
        printf("Uncertain\n");
    } 
    else {
        for (int i = 0;i < len / 2;++i) {
            char t = n[i];
            n[i] = n[len-1-i];
            n[len-1-i] = t; 
        }
        long long count = len - 1;
        char b = n[count];
        for (long long i = len-1;i >= 0;--i) {
            if (n[i] != '0') {
                count = i;
                //printf("count = %lld\n", count);
                b = n[i];
                break;
            }
        }
        for (long long i = count;i < len-1;++i) {
            n[i] = n[i+1];
        }
        n[len-1] = b;
        //printf("%s\n", n);
        memset(n1,0,sizeof(n1));
        int c1 = 0;
        int num1;
        int j = 0;
        for (int i = len - 2;i >= 0;--i) {
            if (i == len-2) {
                num1 = ((n[i] - '0') + (n[len-1] - '0') + c1) % 10;
                c1 = ((n[i] - '0') + (n[len-1] - '0') + c1) / 10;                       }
            else {
                num1 = ((n[i] - '0') + c1) % 10;
                c1 = ((n[i] - '0') + c1) / 10;
            }
            //printf("c1 = %d\n", c1);
            n1[j++] = num1 + '0';
        }
        if (c1) {
            n1[j++] = c1 + '0';
        }
        n1[j] = '\0';
        long long len1 = strlen(n1);
        for (long long i = 0;i < len1 / 2;++i) {
            char t = n1[i];
            n1[i] = n1[len1-1-i];
            n1[len1-1-i] = t;   
        }
        printf("%s\n", n1);
    }
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        memset(n,0,sizeof(n));
        scanf("%s", n);
        long long len = strlen(n);
        sort(n,n+len);
        //printf("%s\n", n);
        result(n,len);
    }
    return 0;
}

此题题意简单,但要用到相当多的知识,而且还要注意细节(比如:进位)。
此题可以有更加优化的方法。

你可能感兴趣的:(hdoj5718 Oracle (BestCoder 2nd Anniversary 1001))