C++代码 回文素数
生活随笔
收集整理的這篇文章主要介紹了
C++代码 回文素数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:
??輸入任意兩個正整數m<n,求得兩個數之間的所有回文素數并輸出。
參考代碼
#include <iostream> using namespace std;int get_length(int x);int main() {int m = 0, n = 0;int count = 0;//int temp = 0;bool NotSuShu;std::cin >> m >> n;for (int i = m; i <= n; i++){NotSuShu = false;for (int j = 2; j < i / 2; j++){if (i%j == 0){NotSuShu = true;break;}}if (!NotSuShu){int huiwen = 1;int l = get_length(i);if (l > 1){int * arry = new int[l];int j,temp=i;for (j = 0; j < l; j++) {arry[j] = temp % 10;temp = temp / 10;}for (j = 0; j <= l / 2; j++)if (arry[j] != arry[l - j - 1]) {huiwen = 0;break;}free (arry);}if (huiwen)cout << i << endl;}}cout << endl; }int get_length(int x) {int leng = 0;while (x){x /= 10;leng++;}return leng; }例如,輸入
輸出結果如下:
總結
以上是生活随笔為你收集整理的C++代码 回文素数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序蓝牙调试助手源码
- 下一篇: 原创:康熙错写1字,306年来为何无人改