HZNU 2154 ldh发奖金【字符串】

本文解析了一道关于整数拆分的算法题目,介绍了判断整数是否可拆分的条件及最优拆分策略,并提供了AC代码实现。通过高位数字优先原则,实现了将整数拆分为两个新的整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

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;
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值