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 <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).
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 <cstdio>
#include <cstring>
#include <algorithm>
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;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者/胄宁 什么是爱? 是尽情的等待, 亦或是放手后离开? 什么是伤害? 是残忍的拒绝, 亦或是眼睁睁的无奈? 年...
    胄宁阅读 389评论 0 2
  • 看看表,还早,想着接儿子放学之前把饭做好,他回家就可以直接吃了!没成想,差点让家里面失火! 事情...
    如霞_472c阅读 180评论 0 0
  • 每到空闲时,我就会胡思乱想很多事,有时悲观,有时乐观。每一天,每做一件事,每说一句话,我都感觉自己在成长,在成熟。...
    今夕何日兮阅读 71评论 0 0
  • 走进孤独,才能走出孤独。 五个人里,如果两两分组,会有一个多余,而我不幸是多余的那个。 我们并不是刻意走进孤独。但...
    鞋呢阅读 364评论 6 5
  • 大学宿舍。床下的桌子,还有小书架。 小时候常用的文具。铅笔和削笔刀。
    小猴子不会爬树阅读 252评论 0 3