A

来源:互联网 发布:c语言switch case语句 编辑:程序博客网 时间:2024/06/09 19:22

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 nn 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 TT (1≤T≤10)(1≤T≤10), which denotes the number of test cases.

For each test case, the single line contains an integer nn (1≤n<1010000000)(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.

题意:给你一个数字,里面的数字可以任意打乱顺序,然后分成两个正整数!(注意是正整数!)然后找出累加之后的最大结果。如果不能,输出…

关键字:正整数、任意打乱、累加

分析1:存数据+大数运算

分析2:存数据+特判

皆可!

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int maxn = 1e7 + 10;#define ll long long intchar str[maxn];int a[12];int dp[maxn];int main(){    int n;    scanf("%d", &n);    while (n--)    {        memset(a, 0, sizeof(a));        scanf("%s", str);        int len = strlen(str);        if (len == 1)        {            printf("Uncertain\n");            continue;        }        for (int i = 0; i < len; i++)        {            a[str[i] - '0']++;        }        int min1 = 0;        for (int i = 1; i <= 9; i++)        {            if (a[i])            {                a[i]--;                min1 = i;                break;            }        }        /*        dp[0] = 0;        int k = 1;        for (int i = 9; i >= 0; i--)            for (int j = 0; a[i] > 0; j++)            {                dp[k++] = i;                a[i]--;            }        dp[k - 1] += min1;        for (int i = k - 1; i > 0; i--)        {            dp[i - 1] += dp[i] / 10;            dp[i] = dp[i] % 10;        }        if (dp[0] == 0)        {            for (int i = 1; i < k; i++)            {                printf("%d", dp[i]);            }            printf("\n");        }        else        {            for (int i = 0; i < k; i++)            {                printf("%d", dp[i]);            }            printf("\n");        }        */        memset(dp, 0, sizeof dp);        int k = 0;        for (int i = 0; i <= 9; i++) {            while (a[i]) {                a[i] --;                dp[k++] = i;            }        }        int u = 0;        dp[0] += min1;        u = dp[0] / 10;        dp[0] = dp[0] % 10;        for (int i = 1; i < maxn; i++) {            dp[i] = dp[i] + u;            u = dp[i] / 10;            dp[i] = dp[i] % 10;        }        int flag = 0;        for (int i = maxn - 1; i >= 0; i--)        {            if (dp[i])            {                flag = i;                break;            }        }        if (flag == 0)        {            printf("Uncertain\n");            continue;        }        for (int i = flag; i >= 0; i--)        {            printf("%d", dp[i]);        }        printf("\n");    }    return 0;}
原创粉丝点击