1.5 29: Number inversion

Description
Given an integer, please invert the digits of the number to get a new number. The new number should also satisfy the common form of integers, that is, unless the original number is given as zero, the highest digit of the new number obtained after inversion should not be zero (see Example 2).

Input
Input a total of 1 line, an integer N.

-1,000,000,000 ≤ N≤ 1,000,000,000.
Output There
is 1 line of output, an integer, which represents the new number after inversion.
Sample input
Sample #1:
123

Sample #2:
-380
sample output
Sample #1:
321

Example #2:
-83

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    
    
	int i,N,temp,revert=0;
	bool b = true; // 假设是正整数
	cin >> N;
	if (N < 0){
    
    
		b = false;
		N = -N;
	} 
	while (N > 0){
    
    
		temp = N % 10;
		N /= 10;
		revert = revert*10 + temp;
	} 
	if (!b) revert = -revert;
	cout << revert << endl;
	return 0;
}
n = int(input())
b = 0
r = 0
if n<0:
    b = 1
    n = -n
while n:
    t = n%10
    n = n//10
    r = r*10 + t
if b==1:
    print(-r)
else:
    print(r)

Guess you like

Origin blog.csdn.net/yansuifeng1126/article/details/112988196