在vb中利用隨機函式產生10 99之間的隨機整數,找

時間 2021-08-30 10:29:12

1樓:玫瑰為你盛開

正好寫過這個哦,也給你好啦

以下**不需要任何控制元件

private sub form_activate()

dim a(1 to 100) as integer, prime(1 to 100) as integer, np as integer, sp as integer

'a陣列存放100個隨機整數,prime陣列存放素數,np存放素數個數,sp存放素數總和

randomize

np = 0

sp = 0

print "100個隨機整數為"

for i = 1 to 100

a(i) = 10 + int(90 * rnd)

print a(i);

if i mod 10 = 0 then print

if isprime(a(i)) then

np = np + 1

prime(np) = a(i)

sp = sp + a(i)

end if

next

print

print "包含的素數依次為"

for i = 1 to np

print prime(i);

if i mod 10 = 0 then print

next

print

print "素數個數為"; np; ",素數總和為"; sp

end sub

function isprime(byval num as integer) as boolean '判斷是否為素數的函式

if num < 2 then isprime = false: exit function

dim i as integer

for i = 2 to sqr(num)

if (num mod i) = 0 then

isprime = false

exit function

end if

next i

isprime = true

end function

2樓:

請問,10到99之間有那麼多個質數嗎,總共才90個資料的呀!

隨機函式產生100個10-99之間的隨機整數

3樓:百度文庫精選

內容來自使用者:範衛超10

利用隨機函式產生100個10-99之間的隨機整數,找出其中的奇數和素數,並將奇數按由小到大的順序排列。

private sub command1_click()dim a(1 to 100) as integer, b() as integer

show

redim b(100) as integerrandomize timer

print "隨機產生的資料是:"

for i = 1 to 100

a(i) = int(rnd * 90 + 10)print a(i); " ";

if i mod 10 = 0 then printnext i

print "找到的素數是:"

for i = 1 to 100

if fnprime(a(i)) thenprint a(i); " ";

c = c + 1

if c mod 10 = 0 then printend if

next i

print

print "找到的奇數是:"

for i = 1 to 100

if a(i) mod 2 = 1 thenk = k + 1

b(k) = a(i)

end if

next i

redim preserve b(k)

for i = 1 to k

print b(i); " ";

d = d + 1

if d mod 10 = 0 then printnext i

for i = 1 to k - 1

for j = i + 1 to k

if b(i) > b(j) then

t = b(i): b(i) = b(j): b(j) = tend if

next j

next i

print

print "排序結果:"

for j = 1 to k

print b(j);

g = g + 1

if g mod 10 = 0 then printnext j

end sub

private sub form_load()end sub

function fnprime(m as integer) as boolean

dim e as integer, f as booleanf = true

for e = 2 to m - 1

if m mod e = 0 then

vb利用隨機函式rnd生成10個10-99的整數,並計算其中的素數個數。(求大神看看**錯了?)

4樓:用著追她

1、首先bai,我們

給主函式du先定一個函式。

2、然zhi後dao我們編寫

版for迴圈語句。

3、然後我們編寫輸權出語句及範圍。

4、然後我們開始執行測試。

5、這句警告沒關係,如果不想看到可以新增呼叫數學函式。

6、這時我們便可以看到效果了。

5樓:匿名使用者

我給你優化了一下,由於最大99,所以只需要判斷到9就可以了,一回旦j=10,則為素數答

private sub command1_click()randomize

dim i as integer, j as integer, n as integer, nl as integer, st as integer

for i = 1 to 10

n = int(rnd * 90 + 10)st = st & n & " "

for j = 2 to 9

if n mod j = 0 then exit fornext j

if j = 10 then

nl = nl + 1

end if

next i

text1.text = st

text2.text = nl

end sub

6樓:聽不清啊

已經copy改好了:

private sub command1_click()randomize

dim i as integer, j as integer, n as integer, nl as integer, st as string

for i = 1 to 10

n = int(rnd * 99 + 10)st = st & n & " "

for j = 2 to n - 1

if n mod j = 0 then exit fornext j

if j = n then

nl = nl + 1

end if

next i

text1.text = st

text2.text = nl

end sub

7樓:匿名使用者

n = int(rnd * 90 + 10)

8樓:當你遇到豆腐

st as string

vb程式:利用隨機函式產生100個10~99之間的隨機整數,找出其中的素數,並將這些素數求和。

9樓:匿名使用者

下面是我給你寫的程式,需要新增四個文字框,三個按鈕,兩個標籤,其中文字框text1和text2的multiline屬性設定成true,scrollbars屬性可以設定成2或者不用設定。你要是對程式有什麼疑問也可以留個qq,我把工程和窗體檔案發給你。

'定義全域性陣列、變數

dim s(1 to 100) as integer, t(1 to 100) as integer, n%

'結束程式

private sub command3_click()unload me

end sub

private sub form_load()randomize

end sub

'產生100個10-99的隨機整數並存放在陣列s中private sub command1_click()dim i%

text1 = ""

for i = 1 to 100

s(i) = 89 * rnd + 10

next

for i = 1 to 100

text1 = text1 & s(i) & " "

if i mod 10 = 0 then

text1 = text1 + vbcrlf + vbcrlfend if

next

end sub

'顯示全部素數以及個數並求和

private sub command2_click()dim sum%

text2 = ""

for i = 1 to 100

ss (i)

next

for i = 1 to n

text2 = text2 & t(i) & " "

sum = sum + t(i)

if i mod 10 = 0 then

text2 = text2 + vbcrlf + vbcrlfend if

next

text3 = n

text4 = sum

n = 0

end sub

'判斷素數子過程

public sub ss(k as integer)dim i as integer

if s(k) < 1 or s(k) = 1 thengoto over

end if

for i = 1 to s(k) - 2if s(k) mod (i + 1) = 0 thengoto over

end if

next

n = n + 1

t(n) = s(k)

over:

end sub

請參考~~

vb編寫一個程式,利用隨機函式rnd生成10個10-99之間的整數,並計算其中素數的個數 10

10樓:匿名使用者

n= [ 10+int(90*rnd) ]st = [ st + " " + n ]forj=2 to [ n ]

[ n1=n1+1 ]

[ exit for ]

text1.text=[ st ]text2.text= [ n-n1 ]

11樓:

private sub command1_click()dim i as integer, j as integer, n as integer, n1 as integer, st as string

for i = 1 to 10

n =_

屬st = _

for j = 2 to _

if n mod j = 0 then_

next

if j > sqr(n) then

_end if

next

text1.text = _

text2.text = _

end sub

用隨機函式產生10個互不相同的兩位整數,存放到一維陣列中.並輸出其中的素數.

12樓:wonder想知道

有一點瑕疵,生成兩位數的地方有點錯誤,判斷有無重複數的地方也有點錯誤,而且可以進行簡化。修改了的地方我做了註釋。(貼上過來時出錯了,我重新貼上了下)

#include

#include

#include

#include

using namespace std;

void main()

cout<

} 執行結果:

所產生的10個互不相同的二位隨機整數為:

40 54 89 65 56 22 63 94 17 59其中的素數為: 89 17 59

請按任意鍵繼續. . .

vb程式設計 利用隨機函式產生10 99之間的隨機整數

option explicit private sub command1 click dim a 80 as integer,b as integer,nb as integer dim i as integer,j as integer,t as integer 隨機生成80個隨機數 text1....

用vb編寫利用隨機函式產生10 99之間(包括10和

dim n m,a 19 max max 0 for n 0 to 19 a n int rnd 90 10 生成20個10 99的隨機數 next n for n 0 to 18 for m n 1 to 19 if a n a m then swap a n a m 排序從小到大end if n...

VB用隨機函式產生兩位數的整數,存於4行5列的二維陣列中,並求出其中的最大元素

option explicit option base 1 dim a 80 i k t b 80 private sub command1 click me.cls dim j as integer randomize for i 1 to 80 a i int rnd 90 10next i k...