当前位置:网站首页><Sicily>1000. 数字反转

<Sicily>1000. 数字反转

2022-06-23 12:01:00 梦飞

原题如下

1000. 数字反转 Time Limit: 1sec Memory Limit:256MB

Description

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。

Input

输入共 1 行,一个整数N。 -1,000,000,000 ≤ N≤ 1,000,000,000。

Output

输出共 1 行,一个整数,表示反转后的新数。

Sample Input

样例1:
123
样例2:
-380

Sample Output

样例1:
321
样例2:
-83

思路

其实就是不断把数字的低位放到高位,可以通过不断取余、乘10、除10来完成。

代码

#include <iostream>

using namespace std;

int main(){
	int input;
	cin >> input;
	int output = 0;
	int r = input % 10;
	while (input != 0){
		r = input % 10;
		input = input / 10;
		output = output * 10 + r;
	}
	cout << output;
} 
原网站

版权声明
本文为[梦飞]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/2028383