C++为什么?

P1307 [NOIP2011 普及组] 数字反转

constructor @ 2017-09-09 18:19:21

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    cin>>str;
    int i=str.length()-1;
    if(str[0]=='-'){cout<<"-";i--;}
    for(;str[i]=='0';i--);
    for(;i>=0;i--)
    {
    if(str[i]!='-')cout<<str[i];
    }
    return 0;
}
//九十分,为什么?

by jiaowohaoge @ 2017-09-12 13:27:20

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int l=0;
    int k=s.length()-1;
    if (s[0]=='0')  cout<<s;
    else {
        if (s[0]=='-') {
        cout<<s[0];
        l=1;}
        while ((k>0)&&(s[k]=='0')){
         k--;
        }
        for (int i=k;i>=l;i--)
        {
         if (((s[i]>='0')&&(s[i]<='9')))cout<<s[i];
        }    
    }
    return 0;
}
我们方法差不多,你看看

by jiaowohaoge @ 2017-09-12 13:28:06

if(str[0]=='-'){cout<<"-";i--;}

这里不是 i--


|