Determine if a number is a palindrome

  • To determine whether a number is a palindrome, for an arbitrary input integer, compare whether the first digit and the last digit are equal, and whether the second digit and the penultimate digit are equal. . . . Until the n/2 comparison is completed, if there is an inequality, return false, otherwise return true.

  • The complete C++ code is as follows:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool help(int s)
{
vector<int> nums;
bool result = true;
while (s > 0)
{
int temp = s % 10;
s = s / 10;
nums.push_back(temp);
}
int n = nums.size();
for (int i = 0; i < n / 2; i++)
{
if (nums[i] != nums[n - 1 - i])
result = false;
}
return result;
}
int main()
{
int s;
bool result;
cin >> s;
result = help(s);
cout << result;
system("pause");
return 0;
}


Guess you like

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