80分求助

P1307 [NOIP2011 普及组] 数字反转

YuanHaoYu6315 @ 2023-07-30 16:11:28

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin >> s;
    if(s == "0"){
        cout << 0 << endl;
        return 0;
    }
    if(s[0] == '-'){
        cout<<"-";
        if(s[s.size()-1] == '0'){
            for(int i = s.size() - 2;i >= 1;i--){
                cout << s[i];
            }
            cout << endl;
        }
        else{
            for(int i = s.size() - 1;i >= 1;i--){
                cout << s[i];
            }
            cout << endl;
        }
    }
    else{
        if(s[s.size()-1] == '0'){
            for(int i = s.size() - 2;i >= 0;i--){
                cout << s[i];
            }
            cout << endl;
        }
        else{
            for(int i = s.size() - 1;i >= 0;i--){
                cout << s[i];
            }
            cout << endl;
        }
    }
}

by wzb13958817049 @ 2023-07-30 16:18:59

@YuanHaoYu6315

if(s[s.size()-1] == '0'){

只判断了末尾有一个0的情况,如果是100的话,反转后应是1,而你只判断了一个0,应该用while或for来判断


by YuanHaoYu6315 @ 2023-07-30 16:19:55

@wzb13958817049 感谢


|