75分,第二个点wa,不知道为什么错,求求大佬

P1015 [NOIP1999 普及组] 回文数

Colas_miao_ @ 2023-02-14 20:47:57

悬赏关注

#include<bits/stdc++.h>
using namespace std;
int n,s=0;
string add(string a,string b)
{
    string c;
    int t,i,j,lena,lenb,x=0;
    lena=a.size();
    lenb=b.size();
    for(i=0;i<lena/2;i++) swap(a[i],a[lena-i-1]);
    for(i=0;i<lenb/2;i++) swap(b[i],b[lenb-i-1]);
    for(i=0;i<lenb&&i<lena;i++)
    {
        t=a[i]-'0'+b[i]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lena)
    {
        t=a[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lenb)
    {
        t=b[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    if(x>0)
    c.push_back(x+'0');
    int len=c.size();
    for(int i=0;i<len/2;i++)
    {
        swap(c[i],c[len-i-1]);
    }
    return c;
}
bool check(string a)
{
    int len=a.size();
    for(int i=0;i<len;i++)
    {
        if(a[i]!=a[len-i-1]) return 0;
    }
    return 1;
}
int main()
{
    string a,b,c;
    cin>>n>>a;
    if(check(c)==0)
    {
        cout<<"STEP=0";
        return 0;
    }
    int i=1;
    while(i<=30)
    {
        s++;
        reverse(a.begin(),a.end());
        b=a;
        reverse(a.begin(),a.end());
        a=add(a,b);
        if(check(a)==1)
        {
            printf("STEP=%d",s);
            return 0;
        }
        ++i;
    }
    cout<<"Impossible!";
    return 0;
}

by Sun_Email @ 2023-02-14 21:01:39

if(check(c)==0)
{
    cout<<"STEP=0";
    return 0;
}

话说这一段是什么意思,这个c不是空串吗?

@I_is_Chen_Yang


by Sun_Email @ 2023-02-14 21:02:23

是check(a)吧


by Colas_miao_ @ 2023-02-14 21:03:10

。。。咳咳,搞错了,重发遍

#include<bits/stdc++.h>
using namespace std;
int n,s=0;
string add(string a,string b)
{
    string c;
    int t,i,j,lena,lenb,x=0;
    lena=a.size();
    lenb=b.size();
    for(i=0;i<lena/2;i++) swap(a[i],a[lena-i-1]);
    for(i=0;i<lenb/2;i++) swap(b[i],b[lenb-i-1]);
    for(i=0;i<lenb&&i<lena;i++)
    {
        t=a[i]-'0'+b[i]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lena)
    {
        t=a[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lenb)
    {
        t=b[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    if(x>0)
    c.push_back(x+'0');
    int len=c.size();
    for(int i=0;i<len/2;i++)
    {
        swap(c[i],c[len-i-1]);
    }
    return c;
}
bool check(string a)
{
    int len=a.size();
    for(int i=0;i<len;i++)
    {
        if(a[i]!=a[len-i-1]) return 0;
    }
    return 1;
}
int main()
{
    string a,b,c;
    cin>>n>>a;
    if(check(a)==0)
    {
        cout<<"STEP=0";
        return 0;
    }
    int i=1;
    while(i<=30)
    {
        s++;
        reverse(a.begin(),a.end());
        b=a;
        reverse(a.begin(),a.end());
        a=add(a,b);
        if(check(a)==1)
        {
            printf("STEP=%d",s);
            return 0;
        }
        ++i;
    }
    cout<<"Impossible!";
    return 0;
}

by Colas_miao_ @ 2023-02-14 21:04:46

#include<bits/stdc++.h>
using namespace std;
int n,s=0;
string add(string a,string b)
{
    string c;
    int t,i,j,lena,lenb,x=0;
    lena=a.size();
    lenb=b.size();
    for(i=0;i<lena/2;i++) swap(a[i],a[lena-i-1]);
    for(i=0;i<lenb/2;i++) swap(b[i],b[lenb-i-1]);
    for(i=0;i<lenb&&i<lena;i++)
    {
        t=a[i]-'0'+b[i]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lena)
    {
        t=a[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lenb)
    {
        t=b[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    if(x>0)
    c.push_back(x+'0');
    int len=c.size();
    for(int i=0;i<len/2;i++)
    {
        swap(c[i],c[len-i-1]);
    }
    return c;
}
bool check(string a)
{
    int len=a.size();
    for(int i=0;i<len;i++)
    {
        if(a[i]!=a[len-i-1]) return 0;
    }
    return 1;
}
int main()
{
    string a,b,c;
    cin>>n>>a;
    if(check(a)==1)
    {
        cout<<"STEP=0";
        return 0;
    }
    int i=1;
    while(i<=30)
    {
        s++;
        reverse(a.begin(),a.end());
        b=a;
        reverse(a.begin(),a.end());
        a=add(a,b);
        if(check(a)==1)
        {
            printf("STEP=%d",s);
            return 0;
        }
        ++i;
    }
    cout<<"Impossible!";
    return 0;
}

虽然还没对


by Sun_Email @ 2023-02-14 21:05:21

@I_is_Chen_Yang

你N=16的时候特判了吗('A')


by Colas_miao_ @ 2023-02-14 21:06:01

不知道怎么判


by Sun_Email @ 2023-02-14 21:09:21

额 反正N=16的时候也就ABCDEF

然后的话就这么写:

string a,b;
int x=0;
for(int i=0,t;i<a.length();++i){
    if('0'<=a[i]&&a[i]<='9'&&'0'<=b[i]&&b[i]<='9'){
        t=a[i]-'0'+b[i]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    if('0'<=a[i]&&a[i]<='9'&&'A'<=b[i]&&b[i]<='Z'){
        t=a[i]-'0'+b[i]-'A'+10+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    ...
} 

没打全是因为 我懒得写 重复了


by Sun_Email @ 2023-02-14 21:09:33

@I_is_Chen_Yang


by Colas_miao_ @ 2023-02-14 21:13:45

#include<bits/stdc++.h>
using namespace std;
int n,s=0;
string add(string a,string b)
{
    string c;
    int t,i,j,lena,lenb,x=0;
    lena=a.size();
    lenb=b.size();
    for(i=0;i<lena/2;i++) swap(a[i],a[lena-i-1]);
    for(i=0;i<lenb/2;i++) swap(b[i],b[lenb-i-1]);
    for(i=0;i<lenb&&i<lena;i++)
    {
        if('0'<=a[i]&&a[i]<='9'&&'A'<=b[i]&&b[i]<='Z'){
            t=a[i]-'0'+b[i]-'A'+10+x;
            x=t/n;
            c.push_back(t%n+'0');
        }
        else 
        {
            t=a[i]-'0'+b[i]-'0'+x;
            x=t/n;
            c.push_back(t%n+'0');
        }
    }
    while(i<lena)
    {
        t=a[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    while(i<lenb)
    {
        t=b[i++]-'0'+x;
        x=t/n;
        c.push_back(t%n+'0');
    }
    if(x>0)
    c.push_back(x+'0');
    int len=c.size();
    for(int i=0;i<len/2;i++)
    {
        swap(c[i],c[len-i-1]);
    }
    return c;
}
bool check(string a)
{
    int len=a.size();
    for(int i=0;i<len;i++)
    {
        if(a[i]!=a[len-i-1]) return 0;
    }
    return 1;
}
int main()
{
    string a,b,c;
    cin>>n>>a;
    if(check(c)==0)
    {
        cout<<"STEP=0";
        return 0;
    }
    int i=1;
    while(i<=30)
    {
        s++;
        reverse(a.begin(),a.end());
        b=a;
        reverse(a.begin(),a.end());
        a=add(a,b);
        if(check(a)==1)
        {
            printf("STEP=%d",s);
            return 0;
        }
        ++i;
    }
    cout<<"Impossible!";
    return 0;
}

为什么还错


by Sun_Email @ 2023-02-14 21:14:53

check(c)==0.........

@I_is_Chen_Yang


| 下一页