有什么错?请大神指教!

P1307 [NOIP2011 普及组] 数字反转

steven_cnyali @ 2016-05-12 21:51:44

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
int main(){
    int a,i=0,j,b[10];
    scanf("%d",&a);
    if (a>0){
        while (a>0){
            i++;
            b[i]=a%10;
            a=a/10;
        }
        for (j=1;j<=i;j++){
            if (b[j]>0 || b[1]!=0) 
                printf("%d",b[j]);
        }
    }
    else if (a==0) printf("%d",a);
    else {
        a=0-a;
        while (a>0){
            i++;
            b[i]=a%10;
            a=a/10;
        }
        printf("-");
        for (j=1;j<=i;j++){
            if (b[j]>0 || b[1]!=0) 
                printf("%d",b[j]);
        }
    }
    return 0;     
}

by chenyihan @ 2016-05-13 16:33:15

全错


by binbang @ 2017-03-23 21:48:46

#include<iostream>
#include<string>
#include <stdlib.h>
using namespace std;
char a[100001];
int top=0;
int main()
{
    string ch;
    string b;
    bool sign=false;
    bool flag=false;
    cin>>ch;
    for(int i=0;i<=ch.length();i++)
    {
        if(ch[i]=='-')    sign=true;
        else a[++top]=ch[i];
    }    
    if(sign==true)    b+="-";
    top--;
    for(top;top>=0;top--)
    {
        if(a[top]!='0'&&flag==false)
        {
            flag=true;
            b+=a[top];    
        }else if(flag==true)
        {
            b+=a[top];
        }
    }
    int i=atoi(b.c_str());
    cout<<i;
    return 0;    
}

|