测样例的时候发现有些回文数不是质数也输出了,判断错了吗?

P1217 [USACO1.5] 回文质数 Prime Palindromes

ninininininini @ 2024-07-06 23:28:12

#include <iostream>
using namespace std;
int if_prime_palindrome(int num) {
    int temp = num, rev = 0;
    while (temp!= 0) {
        rev = rev * 10 + temp % 10;
        temp /= 10;
    }
    if (num == rev) {
        for (int i = 2; i*i <= num; i++) {
            if (num % i == 0) return 0;
            return 1;
        }
    }
    return 0;
}
int main() {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; ++i) {
        if (if_prime_palindrome(i)) {
            cout << i << endl;
        }
    }
    return 0;
}

by 水星湖 @ 2024-07-06 23:54:14

@ninininininini return 1写到循环外面


|