加号放哪里c++

代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long ansfqy = __LONG_MAX__;
int ansnum = 10;

void justdfs(long n, long frequency);
void exceeddfs(char *n, long frequency);

void justdfs(long n, long frequency) {
    if (ansfqy < frequency) {
        return;
    }

    if (n < 10 && (n < ansnum || frequency < ansfqy)) {
        ansnum = n;
        ansfqy = frequency;
        return;
    }

    long temp = 10;
    while (n / temp > 0) {
        long nleft = n / temp;
        long nright = n % temp;
        temp *= 10;
        justdfs(nleft + nright, frequency + 1);
    }
}

void exceeddfs(char *n, long frequency) {
    if (ansfqy < frequency) {
        return;
    }

    for (int i = 1; i < strlen(n); i++) {
        char BIleft[1025], BIright[1025];
        strncpy(BIleft, n, i);
        BIleft[i] = '\0';
        strncpy(BIright, n + i, strlen(n) - i);
        BIright[strlen(n) - i] = '\0';

        long sum = atol(BIleft) + atol(BIright);
        char temp[1025];
        sprintf(temp, "%ld", sum);

        if (strlen(temp) < 19) {
            justdfs(sum, frequency + 1);
        } else {
            exceeddfs(temp, frequency + 1);
        }
    }
}

int main() {
    char n[1025];
    scanf("%s", n);

    if (strlen(n) < 19) {
        justdfs(atol(n), 0);
    } else {
        exceeddfs(n, 0);
    }

    printf("%ld %d\n", ansfqy, ansnum);

    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值