P1999 小菜的25分代码求哪位大佬调一下

P1015 [NOIP1999 普及组] 回文数

NOI_First_Prize @ 2024-08-07 07:41:26

#include<iostream>
#include<cstring>
using namespace std;
char a[3000] , b[3000];
int n ,len;
bool pd()
{
    for(int i = 0;i < len;i++ )
        if(a[i] != a[len - i - 1]) return false;
    return true;
}
void add()
{
    for(int i = 0;i < len;i++)
        b[len - i - 1] = a[i];
    len+=2;
    for(int i = 0;i < len;i++)
    {
        a[i] += b[i];
        if (a[i]>=n) a[i+1]++,a[i]-=n;
    }
    for(;!a[len];) len--;
}
int main()
{
    scanf("%d%s",&n,a);
    len = strlen(a);
    for(int i = 0;i < len;i++)
    {
        if(a[i] >= '0'&&a[i] <= '9') a[i] -= '0';
        else a[i] =a[i] - 'A' + 10;
    }
    int huan = 0;
    while(!pd())
    {
        huan ++;
        if(huan > 30)
        {
            break;
        }

        add();
    }
    if (huan<=30) printf("STEP=%d\n",huan);
    else puts("Impossible!");
    return 0;
}

//照第一遍题解写的


by lovely_codecat @ 2024-08-07 08:25:37

#include<iostream>
#include<cstring>
using namespace std;
char a[3000] , b[3000];
int aa[3000],bb[3000];
int n ,len;
bool pd()
{
    for(int i = 0;i < len;i++ )
        if(aa[i] != aa[len - i - 1]) return false;
    return true;
}
void add()
{
    for(int i = 0;i < len;i++)
        bb[len - i - 1] = aa[i];
//  len+=2;
    for(int i = 0;i < len;i++)
    {
        aa[i] += bb[i];
        if (aa[i]>=n) aa[i+1]++,aa[i]-=n;
    }
    for(;aa[len];) len++;
    //len--;
}
int main()
{
    scanf("%d%s",&n,a);
    len = strlen(a);
    for(int i = 0;i < len;i++)
    {
        if(a[i] >= '0'&&a[i] <= '9') aa[i]=a[i]-'0';
        else aa[i]=a[i] - 'A' + 10;
    }
    int huan = 0;
    while(!pd())
    {
        add();
        huan ++;
        if(huan > 30)
        {
            break;
        }

    }
    if (huan<=30) printf("STEP=%d\n",huan);
    else puts("Impossible!");
    return 0;
}

by lovely_codecat @ 2024-08-07 08:42:33

@NOI_First_Prize 改好了


by NOI_First_Prize @ 2024-08-07 09:05:39

@lovely_codecat 谢谢红名大佬


|