VB程式設計將6 100之間的全部偶數表示為兩個素數之和,在列表

時間 2021-09-01 20:52:08

1樓:匿名使用者

private sub command1_click()dim a as integer, b as integerdim i as integer, j as integerdim k as integer

a = 6

b = 100

list1.clear

for i = a to b step 2for j = 2 to i / 2

if prime(j) and prime(i - j) thenk = k + 1

list1.additem cstr(k) & ": " & cstr(i) & "=" & cstr(j) & "+" & cstr(i - j) '修改這裡的**就行了

end if

next j

next i

label1.caption = "6~100之間有" & cstr(k) & "對素數和"

end sub

'判斷是否為素數

private function prime(n as integer) as boolean

dim i as integer

if n = 1 then

exit function

elseif n = 2 then

prime = true

exit function

elseif n mod 2 = 0 thenexit function

end if

for i = 3 to sqr(n) step 2if n mod i = 0 then exit functionnext i

prime = true

end function

2樓:

此題需要先編寫一個求素數的自定義函式,具體如下:

1.開啟vb,畫一個listbox控制元件,名稱list1,再畫一個label控制元件,名稱label1

2.畫一個command控制元件,名稱command1

3.雙擊command1,開啟**視窗,輸入如下**:

function pdsu(a as integer) as integer

dim b as integer: dim i as integer

if a <= 3 then

pdsu = 1

else

b = a \ 2

for i = 2 to b

if (a mod i) = 0 then

pdsu = 0: exit function

exit for

end if

next i

pdsu = 1

end if

end function

private sub command1_click()

dim m as integer

dim n as integer

dim i as integer

dim s as integer

s = 0

for i = 6 to 100 step 2

for m = 2 to i - 2

n = i - m

if (pdsu(m) = 1) and (pdsu(n) = 1) then

list1.additem str(i) & "=" & str(m) & "+" & str(n): s = s + 1

exit for

end if

next m

next i

label1.caption = "6-100間共有" & str(s) & "對素數和"

end sub

4.如果操作輸入無誤,即可以顯示正常結果,共有48對素數和

說明:解答此題,我用了約10分鐘,給我加分吧

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程式設計的問題

lbound a 取得陣列的下標 ubound a 取得陣列的上標 這樣可以根據資料的上下標確定陣列裡的引數 arr是表示陣列 arr2 4 表示陣列裡有四個引數 step表示是步長,step 1表示在原來的基礎上減1 將timer1的interval屬性設定為100,enabled屬性設定為fal...

VB程式設計的問題

很簡單啊,在窗體上建立兩個按鈕,如下 private subcommand1 click 隨機生成20個正整數 clsdimias integer dims1 asstring dims2 asstring dima 19 asinteger fori 0 to19 a i int rnd 100 ...