0
我寫了一個C++程序,打印所有低於n
的素數,但程序在執行時不斷崩潰。在C++中打印所有低於n的質數(文件崩潰)
#include <iostream>
using namespace std;
bool premier(int x) {
int i = 2;
while (i < x) {
if (x % i == 0)
return false;
i++;
}
return true;
}
int main() {
int n;
int i = 0;
cout << "entrer un entier n : ";
cin >> n;
while (i < n) {
if (n % i == 0 && premier(i))
cout << i;
i++;
}
;
}
調查此問題的正確工具是調試器。 – chris
你用'i == 0'執行'n%i'' –
從'i = 1'開始而不是 –