c++求大佬帮忙

P1015 [NOIP1999 普及组] 回文数

dreamback_2023 @ 2023-10-31 11:50:02


求大佬帮下忙,输入参考输出的是impossible
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define maxlen 2500
#define rmaxlen 3000
string add_s1, add_s2;
//高精加法
int add_a[maxlen], add_b[maxlen], add_c[rmaxlen];
string func_add(string add_s1, string add_s2,int n)
{
    //a重置为0,长度重置为maxlen和rmaxlen 
    memset(add_a, 0, sizeof(int) * maxlen);
    memset(add_b, 0, sizeof(int) * maxlen);
    memset(add_c, 0, sizeof(int) * rmaxlen);

    int len = add_s1.length();
    //逆序存放
    for (int i = 0; i < len; i++)
    {
        add_a[len - 1 - i] = add_s1[i] - '0';
    }
    len = add_s2.length();
    for (int i = 0; i < len; i++)
    {
        add_b[len - 1 - i] = add_s2[i] - '0';
    }
//判断哪个len大
    if (add_s1.length() > len)
    {
        len = add_s1.length();
    }
    //相加
    for (int i = 0; i < len; i++)
    {
        add_c[i] += add_a[i] + add_b[i];
        //到n进位
        if (add_c[i] >= n)
        {
            add_c[i + 1] += add_c[i] / n;
            add_c[i] %= n;
        }
    }

//增长
    if (add_c[len] > 0)
    {
        len++;
    }
    string add_ans;
    for (int i = len - 1; i >= 0; i--)
    {
        add_ans += add_c[i] + '0';
    }
    return add_ans;
}
//倒过来
string change(string t)
{

    int len = t.length();
    string ans;
    for (int i = len - 1; i >= 0; i--)
    {
        ans += t[i] + '0';
    }

    return ans;
}
//判断是否是回文数
bool isPalindromes(string t) 
{
    int len = t.length();
    int i = 0;//从两边判断
    int j = len - 1;
    while (len--)
    {
        if (len / 2 < i)
        {
            break;
        }
        if (t[i] != t[j])
        {
            return false; 
        }
        i++;
        j--;
    }
    return true;
}

int main()
{
    string t;
    int n;
    int i = 0;
    //n进制 t是数字
    cin >> n >> t;
    while (!isPalindromes(t))
    {
        string b = change(t);
        if (!isPalindromes(t)) 
        {
            i++;
            t = func_add(t,b,n);
            if (i > 30)
            {
                break;
            }
        }
        else
        {
            break;
        }

    }
    if (i >= 30)
    {
        cout << "Impossible!" << endl;
    }
    else
    {
        cout << "STEP=" << i << endl;
    }

    return 0;
}

by CRZ_AK_DZG @ 2023-10-31 13:15:14

ans += t[i];(change)


by dreamback_2023 @ 2023-10-31 16:53:51

@Carl1264349512 妙手回春啊大夫,但改了之后提交上去还是有一个错误,输入16和AC27,原本是要输出6的,但是的代码输出的是2 万分感谢


by dreamback_2023 @ 2023-10-31 17:12:11

@Carl1264349512 解决了,翻转数字哪里有问题


by CRZ_AK_DZG @ 2023-10-31 20:15:12

应该是高精的问题,我正在看,而且代码有逻辑重复,主函数中的循坏判断过了,里面就不用判断


by CRZ_AK_DZG @ 2023-10-31 20:40:17

string func_add(string add_s1, string add_s2,int n){
    memset(add_a, 0, sizeof(int) * maxlen);
    memset(add_b, 0, sizeof(int) * maxlen);
    memset(add_c, 0, sizeof(int) * rmaxlen);
    int len = add_s1.length();
  //这里
    for (int i = 0; i < len; i++){
        if(add_s1[i]>='0' && add_s1[i]<='9') add_a[len - 1 - i] = add_s1[i] - '0';
        else add_a[len - 1 - i] = add_s1[i] - 55;
    }
    len = add_s2.length();
    for (int i = 0; i < len; i++){
        if(add_s2[i]>='0' && add_s2[i]<='9') add_b[len - 1 - i] = add_s2[i] - '0';
        else add_b[len - 1 - i] = add_s2[i] - 55;
    }
  //一处错误
    if (add_s1.length() > len) len = add_s1.length();
    for (int i = 0; i < len; i++){
        add_c[i] += add_a[i] + add_b[i];
        if (add_c[i] >= n){
            add_c[i + 1] += add_c[i] / n;
            add_c[i] %= n;
        }
    }
    if (add_c[len] > 0) len++;
    string add_ans;
  //这里
    for (int i = len - 1; i >= 0; i--){
        if(add_c[i] < 10) add_ans += add_c[i] + '0';
        else add_ans += add_c[i]-10+'A';
    }   
  //结束
    return add_ans;
}

by CRZ_AK_DZG @ 2023-10-31 20:42:22

注释是告诉你从哪改到那,要注意16进制逆序存放和输出,超过了1~9,下次要注意


by dreamback_2023 @ 2023-11-01 18:33:22

@Carl1264349512 好的,十分感谢


|