判断素数回文数,一本通1408

判断素数回文数
输出从11到n之间的素数

img

img

又错了,每天在犯错的道路上越走越远


#include <iostream>
using namespace std;

bool func1(int n){
    if(n < 2) return false;
    if(n==2 || n==5 || n==7 || n==3){
        return true;
    }
    for(int i = 2;i*i<=n;i++){
        if(n%i==0)
            return false;
    }
    return true;
}

bool func2(int n){
    int data[4]={0};
    int i = 0;
    while(n){
        data[i++] = n % 10;
        n /= 10;
    }
    for(int j = 0;j<i/2;j++){
        if(data[j] != data[i-j-1]){
            return false;
        }
    }
    return true;
}

void test01(){    //测试func1() 
    int count = 0;
    for(int i = 1;i<100;i++){
        if(func1(i)) {
            cout<<i<<' ';
            count++;
        }
        if(count == 5){
            cout<<endl;
            count = 0;
        }
    }
}

void test02(){
    int n;
    cin>>n;
    int cnt = 0;
    for(int i = 11;i<=n;i++){
        if(func1(i) && func2(i)){
            cnt++;
        }
    }
    cout<<cnt;
}
int main(){
//    test01();
    test02();
    return 0;
} 

有帮助记得采纳哟

第21行 for (int i=2; 判断素数从2开始遍历

第26行 int k=x; 后面用的k变量,没用y