为什么输出自动少第一个字符?

P1307 [NOIP2011 普及组] 数字反转

doyo @ 2017-08-12 11:04:15

#include<cstdio>
#include<cmath>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int x,i=0;
    string s;
    scanf("%d",&x);
    int n=abs(x);
    while(n!=0)
  {
        s+=n%10+48;
        n/=10;
}
   while(s[i]=0)
    {
          s[i]='l';
           i++;
       }
    if(x<0) 
    {
      printf("%c",'-');
    }
    for(int i=0;i<=s.length();i++)
    if(s[i]!='l') printf("%c",s[i]);
    return 0;
}

by jzy_go @ 2017-08-12 20:44:50

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    string s;
    int o=0;
    bool bo=1;
    cin>>s;
    if(s[0]=='-')
    {
        cout<<'-';
        o=1;
    }
    for(int i=s.size()-1;i>=o;i--)
    {
        if(bo&&s[i]=='0')
        continue;
        else
        {
            bo=0;
            cout<<s[i];
        }
    }
    cout<<endl;
    return 0;
}

|