50分求助

P1307 [NOIP2011 普及组] 数字反转

NNFeNN @ 2023-05-30 20:48:34

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

by NNFeNN @ 2023-05-30 20:50:22

反转后开头和末尾的1也会掉><……


by Slient_QwQ @ 2023-05-30 20:51:40

不难吧


by Slient_QwQ @ 2023-05-30 20:56:52

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    int n, m = 0;
    cin >> n;
    if (n < 0)cout << "-";
    while (n != 0)
    {
        m = m * 10 + n % 10;
        n /= 10; 
    } 
    cout << m;
    return 0;
} 

by NNFeNN @ 2023-05-31 19:42:54

@cc1if 感谢……><


by Terry2011 @ 2023-06-01 16:55:09

#include <bits/stdc++.h>
using namespace std;
int main(){
    int num;
    cin >> num;
    if(num == 0){
        cout << 0;
        return 0;
    }
    if(num < 0){
        cout << '-';
        num = -num;
    }
    while(num % 10 == 0 && num > 0){
        num = num / 10;
    }
    while(num > 0){
        cout << num % 10;
        num = num / 10;
    }

    return 0;
}

|