题目链接
http://acm.hznu.edu.cn/OJ/problem.php?id=2154
思路
先判断不能拆分的情况
以为需要拆分成两个正整数
所以我们可以知道
只有个位的数字 是不能够拆分的
还有 类似于 100 1000000
这种 在每个数位上 只有一个非0数字的 整数 也是不能够拆分的
然后要考虑如何拆分
其实我们容易想到
数位越高 那么这个数字 就会越大
我们应该要尽量 只拆除一个 个位数字 其他数字 从大到小 排列 组成另一个全新的数字
因为 每一位都是可以随意交换的
这样做对答案的贡献就最大
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const int MOD = 1e9 + 7;
bool comp(char x, char y)
{
return x > y;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
string s;
cin >> s;
sort(s.begin(), s.end(), comp);
int len = s.size();
int vis;
for (int i = len - 1; i >= 0; i--)
{
if (s[i] != '0')
{
vis = i;
break;
}
}
if (vis == 0)
printf("Uncertain\n");
else
{
int num = s[vis] - '0';
s.erase(vis, 1);
len = s.size();
for (int i = len - 1; i >= 0; i--)
{
int temp = s[i] - '0';
num += temp;
s[i] = num % 10 + '0';
num /= 10;
if (num == 0)
break;
}
if (num)
cout << num;
cout << s << endl;
}
}
}
整数拆分算法题解析
本文解析了一道关于整数拆分的算法题目,介绍了判断整数是否可拆分的条件及最优拆分策略,并提供了AC代码实现。通过高位数字优先原则,实现了将整数拆分为两个新的整数。
387

被折叠的 条评论
为什么被折叠?



