0分求助!

P1307 [NOIP2011 普及组] 数字反转

Flower_Bloom @ 2022-08-27 10:40:34

如题,只有RE和WA,请各位dalao帮忙看看。

#include <bits/stdc++.h>
using namespace std;
stack <char> a;
char c[1005];
char ans[1005];
int i = 1;
int main()
{
    while (scanf("%c", &c[i]) ==1)
    {
        i++;
    }
    if (c[1] == 0)
    {
        cout << 0;
    }
    if (c[1] == '-')
    {
        ans[1] = '-';
        for (int j = 2; j < i; j++)
        {
            a.push(c[j]);
            if (a.top() == '0' && c[j + 1] == '0' || a.top() == '0' && c[j + 1] != '0')
            {
                a.pop();
            }
        }
        for (int j = 2; j < i; j++)
        {
            ans[j] = a.top();
            a.pop();
        }
    }
    else
    {
        for (int j = 1; j <= i; j++)
        {
            a.push(c[j]);
            if (a.top() == '0' && c[j + 1] == '0' || a.top() == '0' && c[j + 1] != '0')
            {
                a.pop();
            }
        }
        for (int j = 1; j <= i; j++)
        {
            ans[j] = a.top();
            a.pop();
        }
    }
    for (int j = 1; j <= i; j++)
    {
        printf("%c", ans[j]);
    }
    return 0;
}

by _Hu_Tao @ 2022-08-27 10:44:48

@Mant1e 正确的写法

#include<iostream>
using namespace std;
int main()
{
    int x,neww;
    cin>>x;
    while(x!=0)
    {
        neww=neww*10+x%10;
        x/=10;
    }
    cout<<neww;
}

by _Hu_Tao @ 2022-08-27 10:45:25

@Mant1e 你这样写吧,你那样太麻烦


by _Hu_Tao @ 2022-08-27 10:45:46

@Mant1e 求关注QAQ


by Flower_Bloom @ 2022-08-27 10:47:05

已关,求互关


by _Hu_Tao @ 2022-08-27 10:48:34

@Mant1e 用栈写也不是不行,我不够了解,你可以去题解第一篇你看看


by Flower_Bloom @ 2022-08-27 10:48:52

OK


|