0
我必須爲t個案例找到素數。以下輸入/輸出示例:查找特定區間中的素數
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
注意答案之間的空白。
#include <iostream>
#include <cmath>
bool prime (int x, int y);
using namespace std;
int main()
{
int t, x, y;
cin >> t;
for(int i = 0; i < t; i++)
cin >> x >> y;
for(int i = 0; i < t; i++){
for (int i = x; i <= y; i++)
cout << prime(x, y) << endl;
}
return 0;
}
bool prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++)
if (i % j == 0)
prime = false;
return prime;
}
}
我的程序只有1所有的時間輸出,這是爲什麼?
什麼是「t個案件」? –
t表示測試用例的數量<= 10.例如,在示例中它是2,表示我必須以2個區間輸出質數。 – user3002211
如果範圍內的任何數字爲素數,並且您一直調用該函數並打印結果,則函數返回'true'。也許你應該找到一組素數並打印每一個。 –