VB 高手请进

求1至100之间的质数.
2024-12-20 05:28:39
推荐回答(2个)
回答1:

VB....C++可以不 ....
#include
#include
#include
using namespace std;

int main(int ac, char** av) {
const MAX = 100;
bool sieve[MAX + 1];

fill(sieve, sieve + MAX + 1, true);
sieve[0] = false;
sieve[1] = false;
for (int n=2; n < sqrt((double)MAX + 1); n++) {
if (sieve[n]) {
for (int j=2*n; j < MAX + 1; j+=n)
sieve[j] = false;
}
}
for(int i = 2; i < MAX + 1; i++) {
if(sieve[i])
cout << i << endl;
}
}

回答2:

创建一个命令按钮,直接在窗体上显示,每行显示一个
Private Sub Command1_Click()
Dim i As Integer, j As Integer, flag As Boolean
For i = 2 To 100
flag = True
For j = 2 To Sqr(i)
If (i Mod j) = 0 Then flag = False
Next
If flag Then Print i
Next
End Sub