AcWing Winter Holiday One Question-Day34 Number Reversal

Given an integer, please invert the digits of the number to get a new number.

The new number should also meet the common form of integers, that is, unless the original number is given as zero, the highest digit of the new number obtained after the inversion should not be zero.

Input format
Input a total of 1 line, 1 integer NNN

Output format The
output is 1 line, and 1 integer represents the new number after inversion.

Data range∣
N ∣ ≤ 1 0 9 |N|≤10^9N109
Input sample:

123

Sample output:

321

Input sample:

-380

Sample output:

-83

Analysis: Simulation

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,a[N],k,ans=0,flag;
int main(){
    
    
    cin>>n;
    if(n<0){
    
    
        n=-n;
        flag=1;
    }
    while(n){
    
    
        a[k++]=n%10;
        n/=10;
    }
    for(int i=0;i<k;i++){
    
    
        ans+=pow(10,k-i-1)*a[i];
    }
    if(flag) cout<<"-"<<ans<<endl;
    else cout<<ans<<endl;
}

Guess you like

Origin blog.csdn.net/messywind/article/details/113811703