p1307

P1307 [NOIP2011 普及组] 数字反转

PURE_LOVE @ 2023-11-29 19:27:42


#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
    char x[11];
    char* y = (char*)calloc(11, sizeof(char));
    scanf("%s", x);
    int lx = strlen(x);
    int i = 0;
    if (x[lx - 1] == '0')
    {
        for (i; i < lx - 1; i++)
        {
            y[i] = x[i];
        }
    }
    else
    {
        for (i; i < lx; i++)
        {
            y[i] = x[i];
        }
    }
    if (x[0] == '-')
    {
        printf("-");
    }
    for (i = 11; i >= 0; i--)
    {
        if (y[i] != '0')
        {
            for (i; i >= 0; i--)
            {
                if (y[i] != '-')
                    {printf("%c", y[i]);}
            }
        }
    }
    free(y);
    return 0;
}
```cpp为什么这都AC不了,来大佬解答

by heyx0201 @ 2023-11-29 19:35:43

@PURE_LOVE 你干脆写 C 语言吧这个没有用到一丁点 C++ 的东西


by heyx0201 @ 2023-11-29 19:36:10

除了头文件


by PURE_LOVE @ 2023-11-29 22:45:09

@heyx0201 我是在写c语言啊,c++还没学


by heyx0201 @ 2023-11-30 12:50:55

@PURE_LOVE 《》


by PURE_LOVE @ 2023-11-30 12:59:34

@heyx0201 哦哦哦,刚发帖,要```c这样写是吧


by heyx0201 @ 2023-11-30 13:10:09

@PURE_LOVE 是


by PURE_LOVE @ 2023-11-30 13:45:35

@heyx0201 那能麻烦你帮我看看吗


by Kelly_1 @ 2023-12-22 21:05:19

我也是刚学,动态内存那学的不咋地,不过这道题不用这么复杂吧。我把代码贴在下面了。


by Kelly_1 @ 2023-12-22 21:07:22

#include<stdio.h>
#include<math.h>
int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0, a[10] = { 0 }, b = n;
    if (n < 0)
        n = -n;
        while (n > 0)
        {
            a[i] = n % 10;
            n = n / 10;
            i++;
        }
    n = b;
    int num = 0, c = i - 1;
    for (int j = 0;j < i;j++)
    {
        num = num + a[j] * pow(10, c);
        c--;
    }
    if (n < 0)
        num = -num;
    printf("%d\n", num);
    return 0;
}

|