本机运行可以 洛谷评测运行错误

P1307 [NOIP2011 普及组] 数字反转

就爱无名 @ 2017-03-23 21:27:01

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
char a[1000000001];
int main(){
    int n;
    int i=0;
    int j;
    cin>>n;
    if(n<0){
        cout<<"-";
        n=abs(n);
    }
    while(n>0){
        a[++i]=n%10+48;
        n/=10;
    }
    if(a[1]=='0'){
        for(j=1;j<=i;j++){
            if(a[j]!='0')cout<<a[j];
        }
        for(;j<=i;j++){
            cout<<a[j];    
        }
    }
    else{
        for(j=1;j<=i;j++){
            cout<<a[j];
        }    
    }
    return 0;
}

by binbang @ 2017-03-23 21:43:47

#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;    
}

by 浩子enjoy @ 2017-03-30 20:51:28

#include<iostream>
#include<string>
using namespace std;
int main ()
{
    string a,h;
    int m,k;
    cin>>a;
    m=a.length();
    if (a[0]=='-')
    {
       h[0]='-';
       for(int i=1;i<=m-1;++i)
       {h[i]=a[m-i];}
    }
    else
    {
        for(int i=0;i<=m-1;++i)
        {h[i]=a[m-i-1];}
    }
    for (int i=0;i<=m-1;++i)
    {
        if(h[i]!='-'&&h[i]!='0')
        {
            k=i;
            break;
        }
    }
    if(h[0]=='-')
    {
        cout<<'-';
        for(int i=k;i<=m-1;++i)
        {cout<<h[i];}
    }
    else
    {
        for(int i=k;i<=m-1;++i)
        {cout<<h[i];}
    }
    return 0;
}

|