hdu 5718 Oracle

Oracle

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


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   (1T10) , which denotes the number of test cases.

For each test case, the single line contains an integer  n   (1n<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
题意:将一个数的每个数字重新排列后变成两个不为0的数字相加,使得这两个数相加之和最大。
思路:先将数字当作字符串输入,然后数字排序,找到不为0的位置,然后进行高精度计算。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char c[11111111];
char a[11111111];
char b[11111111];
int d[11111111];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
    	scanf("%s",c);
    	int len=strlen(c);
    	sort(c,c+len);
    	int f;
    	if(len==1||c[len-2]=='0')
    	{
    		cout<<"Uncertain"<<endl;
    		continue;
		}
		int z;
		int i;
    	for(i=0;i<len;i++)
    	{
    		if(c[i]!='0')
    		{
    			f=i;
    			z=c[f]-'0';
    			break;
			}
		}
		int k=0;
		for(i=f;i>0;i--)
		c[i]=c[i-1];
		for(i=1;i<len;i++)
		{
			d[i]=c[i]-'0';
		}
		d[1]+=z;
		for(i=1;i<len-1;i++)
		{
			if(d[i]>9)
			{
				d[i+1]++;
				d[i]-=10;
			}
			else break;
		}
		for(i=len-1;i>=1;i--)
		{
			printf("%d",d[i]);
		}
		printf("\n");
		
		
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值