c++string不懂为什么输入123会输出132231

P1307 [NOIP2011 普及组] 数字反转

why_working @ 2020-02-05 21:49:32

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    string s;
    cin>>s;
    if(s[0]=='-'&&s[s.length()-1]!='0');
    {
        reverse(s.begin()+1,s.end());
        cout<<s;
    }
    if(s[0]=='-'&&s[s.length()-1]=='0')
    {
        reverse(s.begin()+1,s.end());
        s.erase(1);
        cout<<s;
    }
    if(s[0]!='-'&&s[s.length()-1]=='0')
    {
        reverse(s.begin(),s.end());
        s.erase(0);
        cout<<s;
    }
    if(s[0]!='-'&&s[s.length()-1]!='0')
    {
        reverse(s.begin(),s.end());
        cout<<s;
    }
    return 0;
}

输入123 输出132231

输入-120 输出-021

求大佬解惑555


by ZZ作者 @ 2020-02-05 22:01:05

@why_working 您的第一个if那一行最后多了一个分号awa


by ZZ作者 @ 2020-02-05 22:01:54

string s;
    cin>>s;
    if(s[0]=='-'&&s[s.length()-1]!='0');//这里!!
    {
        reverse(s.begin()+1,s.end());

by why_working @ 2020-02-08 21:52:29

谢谢!


|