前三个全wa,求求大佬!

P1015 [NOIP1999 普及组] 回文数

hyghyg520 @ 2021-11-10 20:12:52

#include <cstdio>
#include <cstring>
#include <iostream>
int main()
{
    int n;
    char s[100005];
    scanf("%d %s", &n, s);
    int j = 0,ls = strlen(s);
    int sum = 0;
    int a[100005], b[100005];
    for (int i = 1; i <= ls; i++) {
        if (s[i] <= 9 && s[i] >= 0)
            a[i] = s[i] - '0';
        else a[i] = s[i] - 'A';//十六进制
    }
    for (int i = 1; i <= ls; i++) {//倒转
        b[i] = a[ls +1 -i];
    }
    for(int i = 1;i <= ls;i++) {
        if (b == a)
            printf("STEP=%d", sum);
        else {
            b[i] += a[i];
            b[i + 1] += a[i] / n;
            b[i] %= n;

            sum++;
        }
        if (b[ls + 1] > 0) 
            ls++;
    }
    printf("Impossible!");
    return 0;
}

by 阿丑 @ 2021-11-10 21:21:38

@hyghyg520 字符转 16 进制应写成 a[i] = s[i] - 'A' + 10


by 阿丑 @ 2021-11-10 21:25:58

另外,字符串下标从 0 开始,所以所有 s[i] 应该改成 s[i-1]

以及这个循环

for(int i = 1;i <= ls;i++) {
    if (b == a)
        printf("STEP=%d", sum);
    else {
        b[i] += a[i];
        b[i + 1] += a[i] / n;
        b[i] %= n;

        sum++;
    }
    if (b[ls + 1] > 0) 
    ls++;
}

请考虑一下它的实际意义(


by hyghyg520 @ 2021-11-10 21:58:30

@阿丑 从1开始应该也没问题吧? 这个循环的意思不是如果是回文就输出sum,否则就加自己倒置之后的数吗?


by hyghyg520 @ 2021-11-10 23:21:03

@阿丑 终于过了。。大佬我明白你的意思了,确实是数组存放的问题,然后高精加也打错了。。。。 谢谢大佬!!!


|