首页 >后端开发 >C++ >列表中的所有回文数字是什么?

列表中的所有回文数字是什么?

王林
王林转载
2023-09-10 11:25:021341浏览

列表中的所有回文数字是什么?

在这里我们将看到一个简单的问题。我们必须在给定列表中找到本质上是回文的所有数字。方法很简单,从列表中取出每个数字并检查它是否是回文,然后打印该数字。

算法

getAllPalindrome(arr, n)

Begin
   for each element e in arr, do
      if e is palindrome, then
         print e
      end if
   done
End

示例

#include <iostream>
#include <cmath>
using namespace std;
bool isPalindrome(int n){
   int reverse = 0, t;
   t = n;
   while (t != 0){
      reverse = reverse * 10;
      reverse = reverse + t%10;
      t = t/10;
   }
   return (n == reverse);
}
int getAllPalindrome(int arr[], int n) {
   for(int i = 0; i<n; i++){
      if(isPalindrome(arr[i])){
         cout << arr[i] << " ";
      }
   }
}
int main() {
   int arr[] = {25, 145, 85, 121, 632, 111, 858, 45};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "All palindromes: ";
   getAllPalindrome(arr, n);
}

输出

All palindromes: 121 111 858

以上是列表中的所有回文数字是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除