存心作死换方法做,俩RE俩WA,求助!

P1307 [NOIP2011 普及组] 数字反转

cygnus_beta @ 2021-03-07 21:20:40

蒟蒻作死勿喷www

#include<cstdio>
#include<stack>
#include<cstdlib>
#include<cstring>
using namespace std;

int main(){
    stack<int>sta;
    char str[10],s[2];
    bool flag=false;
    scanf("%s",str);
    if(atoi(str)==0){
        printf("0");
        return 0;
    }
    if(str[0]=='-')flag=true;
    if(flag){
        for(int i=1;i<strlen(str);i++){
            s[0]=str[i];
            sta.push(atoi(s));
        }
    }
    else{
        for(int i=0;i<strlen(str);i++){
            s[0]=str[i];
            sta.push(atoi(s));
        }
    }
    if(flag)printf("-");
    while(!sta.empty()){
        while(sta.top()==0)sta.pop();
        printf("%d",sta.top());
        sta.pop();
    }

    return 0;
}

by 天南星魔芋 @ 2021-03-07 21:23:54

@beta_Cyg 数组小了


by cygnus_beta @ 2021-03-07 21:28:54

现在WA3个点


by 天南星魔芋 @ 2021-03-07 21:29:32

@beta_Cyg 你的0无法入栈


by 天南星魔芋 @ 2021-03-07 21:29:53

例:

405060708

by cygnus_beta @ 2021-03-07 21:33:21

@天南星魔芋 不是入栈的问题,是出栈的,已经解决了,谢谢!


by 天南星魔芋 @ 2021-03-07 21:33:59

@beta_Cyg 刚刚看出来,不用谢


by cygnus_beta @ 2021-03-07 21:34:49

#include<cstdio>
#include<stack>
#include<cstdlib>
#include<cstring>
using namespace std;

int main(){
    stack<int>sta;
    char str[50],s[2];
    bool flag=false;
    scanf("%s",str);
    if(atoi(str)==0){
        printf("0");
        return 0;
    }
    if(str[0]=='-')flag=true;
    if(flag){
        for(int i=1;i<strlen(str);i++){
            s[0]=str[i];
            sta.push(atoi(s));
        }
    }
    else{
        for(int i=0;i<strlen(str);i++){
            s[0]=str[i];
            sta.push(atoi(s));
        }
    }
    if(flag)printf("-");
    flag=true;
    while(!sta.empty()){
        if(flag)while(sta.top()==0)sta.pop();
        flag=false;
        printf("%d",sta.top());
        sta.pop();
    }

    return 0;
}

|