这是否被视为有效的素数生成器。在我看来,这非常有效。是使用流使程序运行更慢吗?
我正在尝试将此提交给SPOJ,它告诉我超出了我的时间限制...
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int testCases, first, second, counter = 0;
bool isPrime = true;
stringstream out;
cin >> testCases;
for (int i = 0; i < testCases; i++) {
// get the next two numbers
cin >> first >> second;
if (first%2 == 0)
first++;
// find the prime numbers between the two given numbers
for (int j = first; j <= second; j+=2) {
// go through and check if j is prime
for (int k = 2; k < j; k++) {
if (j%k == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
out << j << "\n";
}
isPrime = true;
}
out << "\n";
}
cout << out.str();
return 0;
}
编辑:该程序应该在输入中指定的数字之间生成素数。(有关详细信息,请参见此处:Prime Generator Problem)
-托梅克