浙江财经大学
信息管理与工程学院

HDU 5718 Oracle(贪心)

本文由 Ocrosoft 于 2016-08-01 16:25:13 发表

Oracle

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 923    Accepted Submission(s): 407

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 (1≤T≤10), which denotes the number of test cases.
For each test case, the single line contains an integer (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.
 

Solution
不能拆分的情况,有两种,1是只有一位,2是去掉0后只有一位,在判断的时候这两种可以归为一种。例:1和10000。
能拆分的情况,一定是拆分成n-1位和1位的两个数,这个1位的数,一定取除了0以外的最小的数。n-1位的数就像例1一样,从大到小排好。题目要求输出和,进行一下大数加法就可以了。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 10000000+10
using namespace std;
int f[10],a[MAXN];
int main()
{
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        ms(f),ms(a);
        char c;
        while(isdigit(c=getchar()))f[c-'0']++;
        int sum=0;
        for(int i=1;i<=9;i++)sum+=f[i];//计算除0外的数字个数
        if(sum<2)printf("Uncertain\n");
        else
        {
            int t=0,k=0;
            for(int i=1;i<=9;i++)
                if(f[i]){f[i]--;k=i;break;}//寻找1位的数
            for(int i=0;i<=9;i++)
                while(f[i]--)a[t++]=i;//构造len-1位的数
            for(int i=0;i<t;i++)
            {
                int kk=a[i];
                a[i]=(a[i]+k)%10;
                k=(k+kk)/10;
            }
            if(k)a[t++]=k;
            while(t--)printf("%d",a[t]);
            printf("\n");
        }
    }
    return 0;
}

 

欢迎分享与转载,请保留链接与出处。Ocrosoft » HDU 5718 Oracle(贪心)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址