80分求助!!

P1307 [NOIP2011 普及组] 数字反转

zhaoyikuan @ 2024-08-06 13:17:15

简直要疯了,一直显示有2个样例是错的,怎么试数都做到翻转了(第一次因为自己的答案而发帖)

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    string s;
    stack <char> a;
    int start = 0;
    bool f = true;
    cin>>s;
    if(s[0] == '-')
    {
        start = 1;
        cout<<'-';
    }
    for(int i = start; i < s.size(); i++)
    {
        a.push(s[i]);
    }
    if(a.top() == '0' && a.size() != 1) a.pop();
    while(!a.empty())
    {
        if(a.top() == '0' || a.top() == '1' || a.top() == '2' || a.top() == '3' || a.top() == '4' || a.top() == '5' || a.top() == '6' || a.top() == '7' || a.top() == '8' || a.top() == '9')cout<<a.top();
        a.pop();
    }
    return 0;                       

各位大神能帮忙找一下不合理的地方吗


by wscmh @ 2024-08-06 13:34:10

@zhaoyikuan
最后一行少了个大括号
你怎么过的编译
蒟蒻的不解


by zhaoyikuan @ 2024-08-06 13:36:42

忘记复制了,是写了的,不好意思 @wscmh


by zhaoyikuan @ 2024-08-06 13:37:18

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    string s;
    stack <char> a;
    int start = 0;
    bool f = true;
    cin>>s;
    if(s[0] == '-')
    {
        start = 1;
        cout<<'-';
    }
    for(int i = start; i < s.size(); i++)
    {
        a.push(s[i]);
    }
    if(a.top() == '0' && a.size() != 1) a.pop();
    while(!a.empty())
    {
        if(a.top() == '0' || a.top() == '1' || a.top() == '2' || a.top() == '3' || a.top() == '4' || a.top() == '5' || a.top() == '6' || a.top() == '7' || a.top() == '8' || a.top() == '9')cout<<a.top();
        a.pop();
    }
    return 0;  
 }

by wscmh @ 2024-08-06 13:49:38

@zhaoyikuan

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    string s;
    stack <char> a;
    int start = 0;
    cin>>s;
    if(s[0] == '-')
    {
        start = 1;
        cout<<'-';
    }
    for(int i = start; i < s.size(); i++)
    {
        a.push(s[i]);
    }
    while(a.top() == '0' && a.size() != 1) a.pop();
    while(!a.empty())
    {
        cout<<a.top();
        a.pop();
    }
    return 0;     
}

把判断前导零用的if改成while 因为可能不止一个前导零


by zhaoyikuan @ 2024-08-06 13:53:54

@wscmh 感谢您的回答,已AC,结贴


|