vb程式設計題,請高手解答

2021-03-07 15:41:56 字數 5496 閱讀 3371

1樓:匿名使用者

private sub ***mand1_click()dim a() as integer, n as integerdim b() as integer, m as integerdim c() as integer, nc as integerdim i as integer, j as integer, tm as integer

'……前面的關於a b陣列什麼來頭我不寫了 你知道的'定義陣列c大小

nc = m + n

rdim c(nc)

'把陣列a裝入c

for i = 1 to n

c(i) = a(i)

next i

'把陣列b裝入c

for i = 1 to m

c(n + i) = b(i)

next i

'對c進行從大到小排序

for i = 1 to nc - 1

for j = i + 1 to nc

if c(i) < c(j) then

tm = c(i)

c(i) = c(j)

c(j) = tm

end if

next j

next i

end sub

2樓:匿名使用者

option explicit

dim a(4), b(7), c(12)private sub ***mand1_click()dim i%, j%, k%, m%, n%i = lbound(a): j = lbound(b): k = lbound(c)

m = ubound(a): n = ubound(b)while i <= m and j <= nif a(i) > b(j) then

c(k) = a(i): i = i + 1: k = k + 1else

c(k) = b(j): j = j + 1: k = k + 1end if

wend

while k <= m + n - i - j + 1if i > m then

c(k) = b(j): j = j + 1: k = k + 1else

c(k) = a(i): i = i + 1: k = k + 1end if

wend

end sub

3樓:匿名使用者

vb程式設計實現兩個有序陣列的合併,即二路歸併

陣列a和陣列b分別記錄有一些資料,這兩個陣列上的資料都已經由小到大按順序排列好。請用vb編寫一個程式把這此資料合併到同一個陣列之中,並使得合併後的資料全部按由小到大排列。

這就是所謂的「二路歸併」。在編寫這個程式時先進行說明一下,首先,大量資料的輸入用inputbox()函式的方法顯得很煩瑣,特別在除錯階段,每次執行時都要輸入一大堆資料。因此採用了array函式的輸入方法。

為了使大量的資料在輸出時看得清楚,我們在執行窗體上設定了一個文字框,並把文字框的屬性multiline設為true,scorllbars設為2(帶豎直滾動條)。

vb程式**如下:

private sub form_click()

dim a '定義了一個可變型別的陣列

a = array(1, 3, 5, 7, 9, 11)

dim b

b = array(2, 4, 6, 8, 10, 12, 14, 16, 18)

dim c(1000)

pa = 0: pb = 0: pc = 0

ea = ubound(a) '可變陣列a的下標的上界和下界可以用函式ubound()和lbound()得到(若沒說明下標的下界一般是0)

eb = ubound(b)

do until pa > ea and pb > eb

if pa > ea then ta = b(eb) + 1 else ta = a(pa)

if pb > eb then tb = a(ea) + 1 else tb = b(pb)

if ta < tb then

c(pc) = ta

pc = pc + 1

if pa <= ea then pa = pa + 1

else

c(pc) = tb

pc = pc + 1

if pb <= eb then pb = pb + 1

end if

loop

print ea, eb, pc

text1.text = "陣列a的元素:" & vbcrlf

for i = 0 to ea

text1.text = text1.text & a(i) & " "

next i

text1.text = text1.text & vbcrlf & vbcrlf & "陣列a的元素:" & vbcrlf

for i = 0 to eb

text1.text = text1.text & b(i) & " "

next i

text1.text = text1.text & vbcrlf & vbcrlf & "合併後b的元素:" & vbcrlf

for i = 0 to pc

text1.text = text1.text & c(i) & " "

next i

end sub

當然,你可以把這些資料全部複製到一個陣列之中再使用一個排序演算法來處理一次,但這樣的話,原來兩個陣列已經分別排好的順序就沒有利用上,能否利用原來已經排好順序的特點使得演算法高效一些?

4樓:清山媚水

註釋步驟嘛,因為太累了,所以簡寫,估計能看個差不多

在窗體上放一個名為 text1 的textbox控制元件,屬性改為多行和帶滾動條,再放一個按鈕,按鈕的響應程式如下:

private sub ***mand1_click()

const n = 30

const m = 20

dim a(n) as integer, b(m) as integer, c(m + n) as integer

dim i as integer, j as integer, k as integer

for i = 1 to n

a(i) = 3 * i

next i

for j = 1 to m

b(j) = 4 * j

next j

i = n

j = m

p = 0 ' a陣列取淨標誌

q = 0 ' b

for k = 1 to m + n

if a(i) >= b(j) and p = 0 then ' a大取a

c(k) = a(i)

if i > 0 then

i = i - 1

else

p = 1

end if

elseif b(j) > a(i) and q = 0 then ' b大取b

c(k) = b(j)

if j > 0 then

j = j - 1

else

q = 1

end if

end if

if p = 1 then ' a用完取b

c(k) = b(j)

j = j - 1

end if

if q = 1 then ' b用完取a

c(k) = a(i)

i = i - 1

end if

next k

' 輸出至 text1

dim ts as string

for i = 1 to m + n

ts = ts + "i=" + str(i) + ", c(i) = " + str(c(i)) + chr(13) + chr(10)

next i

text1.text = ts

end sub

執行結果如下:

i= 1, c(i) = 90

i= 2, c(i) = 87

i= 3, c(i) = 84

i= 4, c(i) = 81

i= 5, c(i) = 80

i= 6, c(i) = 78

i= 7, c(i) = 76

i= 8, c(i) = 75

i= 9, c(i) = 72

i= 10, c(i) = 72

i= 11, c(i) = 69

i= 12, c(i) = 68

i= 13, c(i) = 66

i= 14, c(i) = 64

i= 15, c(i) = 63

i= 16, c(i) = 60

i= 17, c(i) = 60

i= 18, c(i) = 57

i= 19, c(i) = 56

i= 20, c(i) = 54

i= 21, c(i) = 52

i= 22, c(i) = 51

i= 23, c(i) = 48

i= 24, c(i) = 48

i= 25, c(i) = 45

i= 26, c(i) = 44

i= 27, c(i) = 42

i= 28, c(i) = 40

i= 29, c(i) = 39

i= 30, c(i) = 36

i= 31, c(i) = 36

i= 32, c(i) = 33

i= 33, c(i) = 32

i= 34, c(i) = 30

i= 35, c(i) = 28

i= 36, c(i) = 27

i= 37, c(i) = 24

i= 38, c(i) = 24

i= 39, c(i) = 21

i= 40, c(i) = 20

i= 41, c(i) = 18

i= 42, c(i) = 16

i= 43, c(i) = 15

i= 44, c(i) = 12

i= 45, c(i) = 12

i= 46, c(i) = 9

i= 47, c(i) = 8

i= 48, c(i) = 6

i= 49, c(i) = 4

i= 50, c(i) = 3

vb程式設計題

dim a as integer dim i,j as integer dim temp as string 全部數字 for i 0 to 3 for j 0 to 3 temp temp a i,j tostring next temp temp chr 10 next richtextbox1...

vb程式設計題

private sub form load form1.caption vb程式 form1.forecolor h0000ff end sub private sub form click print vb程式設計基礎 end sub 如果要求只能用一條語句的話,那就在form click 裡面用...

各位高手幫我做道VB程式設計題,謝謝

如果數字是整數可以這樣 x cint x y cint y z cint z 或者用clng x 使用強制轉換函式就可以了.轉換成你希望的各式 摟住瞎說!通過文字框怎麼就不行了 但一定要用函式val 把文字框的內容轉換一下上面這位大哥不錯 x val text1.text y val text2.t...