本程序中最新使用的知识点:
1、常量整数 const int maxnum。
2、math.h 标准库。
3、std::vector 和 std::vector::iterator 类型。
程序如下。
/* 寻找所有的质数,并将寻找到的质数放入 vector 中进行穷举。 * By Ceeji */ const int maxnum = 10000; #include <iostream> #include <vector> #include <math.h> using namespace std; typedef vector<int> intarr; int main() { intarr p = intarr(); bool iszs; int max; for (int i = 1; i != maxnum; ++i) // 从 1 开始寻找质数。 { if (i == 1) { iszs = false; } else { if (i == 2) { iszs = true; } else { max = (int)floor(sqrt((double)i)); iszs = true; for (int j = 2; j != max + 1; ++j) { if (i % j == 0) { // 不是质数。 iszs = false; break; } } } if (iszs) { p.push_back (i); } } } intarr::iterator iter = p.begin (); // 输出 while (iter != p.end ()) { cout << *(iter++) << endl; } return 0; } |
代码显示得有问题呢。
@NetPuter:没问题啊
噗,现在好了……昨天看的时候,全部挤在一行。