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程式設計題隨機產生不重複的A Z之間的英文字母,按字母順序輸出,每行

給你個思路。首先,定義20個陣列。然後在過程里弄個for迴圈,比如for i 1 to 20利用隨機函式,生成65 90之間的函式,賦值到變數,比如 str int 90 65 1 rnd 65 再弄個for迴圈,比如for j 1 to i判斷是否重複 if 陣列名 j str thencall ...

vb程式設計 急求 隨機產生n個1 100之間的數(包含1和

新增乙個command控制項即可,如下 private sub command1 click dim a 5 z,s,max,min as integer print 生成的數字 for i 1 to 5 陣列裡面有5個不同數值a1,a2,a3,a4,a5,此處i的最大值n對應dim中的a n 可以...

VB程式設計的問題,高手幫我

老牛帶你看奇聞 窗體上有三個命令按鈕,command1,command2,command3和乙個text1 如下 dim ys as single private sub command1 click text1.fontsize text1.fontsize int rnd 3 1 command...