自测正常,提交只过了例4

P1015 [NOIP1999 普及组] 回文数

beginner334 @ 2024-11-30 15:55:51

如题,自测这里面的五个数据都过了,但是提交只过#4 这里有五个测试数据

#include <stdio.h>
#include <stdlib.h>
typedef struct _huiwenshu{
    int a;
    int b;
    struct _huiwenshu *shangwei;
    struct _huiwenshu *xiawei;
} intshu;//记录每一位的数
typedef struct _shuliebiao{
    intshu *head;
    intshu *tail;
} qh;//一个数位集合的表
void hui(qh *p,int e)
{
    int w;
    intshu *n,*l;
    for(w=0,n=p->tail,l=p->head;;n=n->shangwei,l=l->xiawei)
    {
        if(n==l)
        {
            w=n->a+l->a,n->a=w%e,w/=e;
            n->shangwei->b+=w;
            break;
        }
        w=n->a+l->a,n->a=l->a=w%e,w/=e;
        n->shangwei->b+=w;
        if(w&&l->shangwei==NULL)
        {
            intshu *m=(intshu*)malloc(sizeof(intshu));
            m->b=0,m->a=w,m->shangwei=NULL,m->xiawei=l;
            p->head=p->head->shangwei=m;
        }
        else if(w) l->shangwei->b=w;
        if(n->shangwei==l) break;
    }//将两数表按照e进制相加
    n=p->tail;
    while(n!=NULL){
        w=n->a+n->b;
        if(w>e-1)
        {
            w%=e;
            if(n->shangwei==NULL)
            {
                intshu *m=(intshu*)malloc(sizeof(intshu));
                m->b=m->a=0,m->shangwei=NULL,m->xiawei=n;
                p->head=p->head->shangwei=m;
            }
            n->shangwei->b++;
        }
        n->a=w;
        n->b=0;
        n=n->shangwei;
    }
}
int judge(qh *p)
{
    intshu *c=p->head,*b=p->tail;
    for(;c->a==b->a&&b!=c&&c->xiawei!=b;c=c->xiawei,b=b->shangwei){}
    if(c==b||c->a==b->a) return 0;
    else return 1;
}//判断是否为回文数
int main()
{
    int i,e,step=0;
    char a[100];
    qh biao;
    biao.head=biao.tail=NULL;
    scanf("%d ",&e);
    fgets(a,100,stdin);
    if(e==16)
        for(i=0;a[i]!='\n';i++)
            if(a[i]>'9')
                a[i]='9'+'A'-a[i]+1;
    for(i=0;a[i]!='\n';i++)
    {
        intshu *m=(intshu*)malloc(sizeof(intshu));
        m->b=0,m->a=a[i]-'0',m->xiawei=NULL;
        if(biao.head==NULL) biao.head=biao.tail=m,m->shangwei=NULL;
        else m->shangwei=biao.tail,biao.tail->xiawei=m,biao.tail=m;
    }//收数进表
    for(;judge(&biao)&&step<31;step++)
        hui(&biao,e);
    if(step<31) printf("STEP=%d",step);
    else printf("Impossible!");
    intshu *w,*r=biao.head;
    while(r!=NULL)
    {
        w=r,r=r->xiawei;
        free(w);
    }
    return 0;
}

by beginner334 @ 2024-12-02 18:00:14

这个下载的WA的数据 2 10011 STEP=4 题解给的代码运行出来的结果也一样, 还是想不清楚哪里有问题

#include <stdio.h>
#include<stdlib.h>
void hui(int b[],int e,int *j)
{
    int i,w,ji=*j-1;
    for(i=0;;i++,ji--)
    {
        w=b[i]+b[ji];
        if(i==ji)
        {
            b[i]=w;
            break;
        }
        b[i]=b[ji]=w;
        if(i+1==ji) break;
    }ji=*j-1;
    for(i=0;i<ji;i++)
        if(b[i]>e-1&&i<ji) b[i+1]+=b[i]/e,b[i]%=e;
    if(b[i]>e-1)
        b[*j]=b[i]/e,b[i]%=e,(*j)++;
}
int judge(int b[],int j)
{
    int i=0;
    while(b[i]==b[j]&&i!=j&&i+1!=j)
        i++,j--;
    if(i==j||b[i]==b[j]) return 0;
    else return 1;
}
int main()
{
    int i,j,e,step=0,b[300];
    char a[105];
    scanf("%d ",&e);
    fgets(a,105,stdin);
    for(i=0;a[i]!='\n';i++)
        if(a[i]>'9')
            a[i]='9'+'A'-a[i]+1;
    for(j=0,i--;a[j]!='\n';j++,i--)
        b[i]=a[j]-'0';
    for(;judge(b,j-1)&&step<31;step++)
        hui(b,e,&j);
    if(step<31) printf("STEP=%d",step);
    else printf("Impossible!");
    return 0;
}

by lty2023 @ 2024-12-06 09:34:37

#include<bits/stdc++.h>
using namespace std;
int main(){
    int p=1,ans=0,i,j,n,x,gg=0,jj,a[105],b[105];
    char c[105];
    cin>>n>>c;
    j=0;
    while(c[j]!=0){
        if((c[j]>='0')&&(c[j]<='9'))a[j+1]=c[j]-'0';
        else a[j+1]=c[j]-'A'+10;
        j++;
    }
    while(ans<=30){
        gg=0;
        i=1;while(i<=j){
            x=a[j-i+1]+a[i]+gg;
            gg=x/n;
            b[j-i+1]=x%n;
            i++;
        }
        if(gg!=0)b[0]=gg;
        if(gg!=0){
            for(i=1;i<=j+1;i++)a[i]=b[i-1];
            j++;
        }
        else for(i=1;i<=j;i++)a[i]=b[i];
        ans++;
        i=1;
        jj=j;
        p=1;
        while(i<=jj){
            if(a[i]!=a[jj])p=0;
            i++;
            jj--;
        }
        if(p){
            cout<<"STEP="<<ans;
            return 0;
        }
    }
    cout<<"Impossible!";
}

|