列表中的所有回文数?
在这里,我们将看到一个简单的问题。我们必须在给定列表中找到所有属于回文性质的数字。方法很简单,从列表中获取每个数字并检查是否为回文,然后打印该数字。
算法
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
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语