感觉我这方法和题解3差不多啊,为啥运行起来这么慢
查看原帖
感觉我这方法和题解3差不多啊,为啥运行起来这么慢
268811
画船听雨楼主2022/1/21 22:49
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
#include<cmath>
using namespace std;

bool zhishu(int x) {
    if (x <= 3) return x > 1;
    if (x % 6 != 1 && x % 6 != 5) return false;
    for (int i = 5; i * i <= x; i = i + 6) {
        if (x % i == 0 || x % (i + 2) == 0)
            return false;
    }
    return true;
}

int main() {
    int a, b, i, j;
    string str;
    cin >> a >> b;      
    if (b >= 9999999)//最大质数不超过一千万
            b = 9999999;
    if (a % 2 == 0)//确保从奇数开始判断
        a++;
    for (i = a; i <= b; i=i+2) {
            stringstream ss;//把数字转化成字符串来判断
            ss << i;
            ss >> str;
            if (i == 11) {
                cout << str << endl;
                continue;
            }

            if (str.size() % 2 != 0) {
                if (str.size() == 1) {
                    if(zhishu(i))
                    cout << str << endl;
                }
                else {
                        for (j = 0; j < str.size() / 2; j++) {
                            if (str[j] != str[str.size() - 1 - j])//判断数字两端是否相等
                                break;
                        }
                        if (j == str.size() / 2)
                            if(zhishu(i))
                            cout << str << endl;

                }
            }
    }

    return 0;
}
2022/1/21 22:49
加载中...