本地可以跑,提交编译失败

P1015 [NOIP1999 普及组] 回文数

Ohuohuo_ @ 2023-09-29 20:03:24

#include<iostream>
using namespace std;
int n;
char s[200];
int a[200],b[200];
int cnt, lens, cur;

bool check()
{
    for (int i = lens+1 - 1; i > 0; i--)
    {
        if (a[i]!=a[lens +1-i])
        {
            return false;
        }
    }
    return true;
}

void add()
{
    //逆序存储入b
    for (int i = 1; i <= lens; i++)
    {
        b[i] = a[lens + 1 - i];
    }
    //两数相加
    cur = 1;
    for (int i = 1; i <= lens; i++)
    {
        a[i] += b[i];
        if (a[i]>=n)
        {
            a[i] -= n;
            a[i + 1]++;
        }
        cur++;
    }
    if (a[cur] == 0 && cur > 1) cur--;
    lens = cur;
    //for (int i = lens; i >= 1; i--)
    //{
    //  cout << a[i];
    //}
    //cout << endl;
}

int main() {
    cin >> n;
    cin >> s;
    lens = strlen(s);
    int flage = 1;
    for (int i = 0; i < lens; i++)
    {
        if (s[i]!=s[lens-1-i]) flage = 0;
    }
    if (flage)
    {
        cout << "STEP=0"; return 0;
    }

    //数据类型转换并存入a中
    for (int i = 0; i < lens; i++)
    {
        a[lens - i] = s[i] - 48;
    }

    while (!check())
    {
        cnt++;
        add();
        if (cnt>30)
        {
            cout << "Impossible!"; return 0;
        }
    }
    cout << "STEP=" << cnt;

    return 0;
}

by Fuction12_ @ 2023-09-29 20:05:29

你提交的是C还是C++


by Fuction12_ @ 2023-09-29 20:06:11

还有你没有包含strlen所需要的头文件


by Fuction12_ @ 2023-09-29 20:07:11

加一个#include<cstring>


by Ohuohuo_ @ 2023-09-29 20:08:48

@F12aaaaaaa 感谢感谢,做题做麻了;)


by Ohuohuo_ @ 2023-09-29 20:13:34

#include<iostream>
#include<cstring>
using namespace std;
int n;
char s[200];
int a[200],b[200];
int cnt, lens, cur;

bool check()
{
    for (int i = lens+1 - 1; i > 0; i--)
    {
        if (a[i]!=a[lens +1-i])
        {
            return false;
        }
    }
    return true;
}

void add()
{
    //逆序存储入b
    for (int i = 1; i <= lens; i++)
    {
        b[i] = a[lens + 1 - i];
    }
    //两数相加
    cur = 1;
    for (int i = 1; i <= lens; i++)
    {
        a[i] += b[i];
        if (a[i]>=n)
        {
            a[i] -= n;
            a[i + 1]++;
        }
        cur++;
    }
    if (a[cur] == 0 && cur > 1) cur--;
    lens = cur;
    //for (int i = lens; i >= 1; i--)
    //{
    //  cout << a[i];
    //}
    //cout << endl;
}

int main() {
    cin >> n;
    cin >> s;
    lens = strlen(s);
    int flage = 1;
    for (int i = 0; i < lens; i++)
    {
        if (s[i]!=s[lens-1-i]) flage = 0;
    }
    if (flage)
    {
        cout << "STEP=0"; return 0;
    }

    //数据类型转换并存入a中
    for (int i = 0; i < lens; i++)
    {
        if (s[i]>='0'&&s[i]<='9')
        {
            a[lens - i] = s[i] - 48;
        }
        else 
        {
            a[lens - i] = s[i] - 'A'+10;
        }
    }

    while (!check())
    {
        cnt++;
        add();
        if (cnt>30)
        {
            cout << "Impossible!"; return 0;
        }
    }
    cout << "STEP=" << cnt;

    return 0;
}

|