C++ 实现整数反转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CV2017/article/details/82927891

在 vs 2010 的调试过程中比较容易理解其实现原理。

#include<iostream>
using namespace std;
int main()
{
	int input;
	cin >> input;
	int output = 0;
	while(input!=0)
	{
		output=output*10+input%10;
		input/=10;
	}
	cout<<output<<endl;
}

 解释:input /= 10 的结果是除去个位数后的值,比如 12345 / 10 的结果为 1234,input % 10 的结果是个位数,比如 12345 % 10  = 5,output 的初始值为 0*10 + 12345%10 = 5,然后再做 while 循环

猜你喜欢

转载自blog.csdn.net/CV2017/article/details/82927891