在特定数量的测试用例中查找特定区间内的素数。查找特定区间内的素数
例子如下: 输入:
2
1 10
3 5
输出:
2
3
5
7
3
5
通知的回答也之间的很小的空间。
这里是我的代码:
#include <iostream>
#include <cmath>
void prime (int x, int y);
using namespace std;
int main()
{
int t, x[10], y[10];
cin >> t;
for (int i = 0; i < t; i++)
//for (int j = 0; j < t; j++)
cin >> x[i] >> y[i];
while (t > 0){
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
t--;
}
}
void prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++){
prime = true;
if (i % j == 0)
prime = false;
}
if (prime == true)
cout << i << endl;
}
cout << endl;
}
这里的输出我得到的,当我使用相同的输入。
1
2
3
5
7
10
3
5
1
2
3
5
7
10
我在做什么错?
使用[偏移Eratothenes筛](http://stackoverflow.com/a/19641049/849891)。 C代码[这里](http://stackoverflow.com/a/9557173/849891)。 –