1168 Prime Day + 字符串 + 质数判断

题目链接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1621700026718310400

题解代码:

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
bool is_prime(int x) {
    if (x < 2) return false;
    for (int i = 2, I = sqrt(x); i < I; i++) {
        if (x % i == 0) return false;
    }
    return true;
}
int main() {
    string str;
    cin >> str;
    int has_no = 0;
    while (str.size()) {
        cout << str << " ";
        int num = stoi(str);
        if (is_prime(num)) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
            has_no = 1;
        }
        str.erase(0, 1);
    }
    if (!has_no) {
        cout << "All Prime!" << endl;
    }
    return 0;
}
posted @ 2023-05-07 18:22  江韵阁  阅读(21)  评论(0)    收藏  举报