c_cpp lazy_primality.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp lazy_primality.cpp相关的知识,希望对你有一定的参考价值。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
using namespace std;

std::string exec(const string &cmd) {
    char buffer[128];
    std::string result = "";
    std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer, 128, pipe.get()) != NULL)
            result += buffer;
    }
    return result;
}

int main() {
    const string pre = "factor ";
    string line;
    getline(cin, line); // skip count line
    while (getline(cin, line)) {
        bool prime = exec(pre + line).size() == (2 * line.size() + 3);
        cout << (prime ? "Prime" : "Not prime") << endl;
    }
}

以上是关于c_cpp lazy_primality.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *