[题解]openjudge-回文素数
提交情况
原题链
解题思路
整体思路:首先生成一个满足为n位的回文数,再判断其是否为素数。
细节问题:我们枚举一个长度为长度的数,将这个数的前
个数放到原数的后方生成新的回文数。example:我们需要一个长度为3为的回文数,那么我们要枚举的数是从
~
。假设我们枚举的一位数为10。那么最后生成的数即是101。
源代码
#include <bits/stdc++.h> using namespace std; int a[1000010]; const int mod = 7; inline int make(int tmp){ int ans = tmp; tmp /= 10; while(tmp > 0){ ans = ans * 10 + tmp % 10; tmp /= 10; } return ans; } inline bool prime(int k){ if(k < 2)return false; if(k == 2)return true; for(int i = 2;i * i <= k;i++){ if(k % i == 0)return false; } return true; } int main(){ int n; cin>>n; if(n % 2 == 0){ if(n == 2){ cout<<1<<endl<<11; return 0; } else { cout<<0; return 0; } } n = (n - 1) / 2; int h = pow(10,n),s = 0; for(int i = h;i <= h * 10 - 1;i++){ int tmp = make(i); if(prime(tmp)) a[++s] = tmp; } cout<<s<<endl; for(int i = 1;i <= s;i++){ cout<<a[i]<<" "; } return 0; }
结束 (ovo);