Integer flip

  • For any given integer, if no overflow or underflow occurred, flip the integer, otherwise return 0.
  • The complete C++ code is as follows:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int help(int n)
{
int b;
int result = 0;
int temp=0;
while (n > 0)
{
b = n % 10;
result = 10 * result + b;
if (result / 10 != temp)
{
return 0;
}
temp = result;
n = n / 10;
}
return result;
}
int main()
{
int n;
cin >> n;
int result;
result = help(n);
cout << result;
system("pause");
return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689208&siteId=291194637