列表中的所有回文数?
在这里,我们将看到一个简单的问题。我们必须在给定列表中找到所有属于回文性质的数字。方法很简单,从列表中获取每个数字并检查是否为回文,然后打印该数字。
算法
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 生日开业蛋糕祝福语简短