Oracle
Accepts: 631
Submissions: 2576
Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
题意:把一串数分为两部分正数,要求和值最大,否则,输出”Uncertain”
分开判断 一位数、两位数,多位数则判断只有一位数不为0的情况,然后进行处理:sort升序排序,只要形成组合 sum = 最大的数+最小的正数【一位数不为0】
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
const int maxn = 10000100;
int n, m, k;
char s[maxn],a[maxn];
int main(){
scanf("%d",&n);
while(n--){
memset(a,'\0',sizeof(a));
scanf("%s",s);
int len = strlen(s);
if(len==1){//一位数
printf("Uncertain\n");
continue;
}
if(len==2 ){//两位数
if(s[0]=='0'||s[1]=='0') printf("Uncertain\n");
else printf("%d\n",s[0]-'0'+s[1]-'0');
continue;
}
sort(s,s+len);//升序
k = 0;
int flag = 1;
int si = -1;
int cnt0 = 0;
for(int i=0;i<len;i++){
if(s[i]!='0' && flag){
si = i;//第一次不为0
flag = 0;
continue;
}
if(s[i]=='0')cnt0++;
a[k++] = s[i];
}
if(cnt0>=len-1) {//只有一个不为0的数
printf("Uncertain\n");
continue;
}
a[0] += s[si]-'0';
if(a[0] > '9'){
a[0] -= 10;
a[1] += 1;
}
m = k;
for(int i=1;i<k;i++){
if(i==k-1&&a[i]>'9') {
m++;
a[i] -= 10;
a[i+1] = '1';
continue;
}
if(a[i] > '9'){
a[i] -= 10;
a[i+1] += 1;
}
}
for(int i=m-1;i>=0;i--){
printf("%c",a[i]);
}
printf("\n");
}
return 0;
}