VB程式設計,求M行N列的矩陣四周元素之和,元素值可以隨機產生,M N從鍵盤輸入

2021-07-01 02:02:45 字數 6322 閱讀 7373

1樓:裘誠

這個是那一章的全部答案

1.設有如下兩組資料:

(1)1,3,5,2,4,18,50,25

(2)5,27,30,35,60,41,87,33

編寫一個程式,把上面兩組資料分別讀入兩個陣列中,然後把兩個陣列中對應下標的元素相加,即1+5,3+27,……,25+33,並把相應的結果放入第三個陣列中,最後輸出第三個陣列的值

**private sub command1_click()

dim i

dim x()

dim y()

dim z(0 to 7)

x = array(1, 3, 5, 2, 4, 18, 50, 25)

y = array(5, 27, 30, 35, 60, 41, 87, 33)

for i = 0 to 7

z(i) = x(i) + y(i)

print z(i);

next i

end sub

執行介面

2.有一個n×m的矩陣,編寫程式,找出其中最大的那個元素所在的行和列,並輸出其值及行號和列號。

**private sub command1_click()

dim a(1 to 10, 1 to 10) as integer

dim m%, n%, x%

for i = 1 to 10

for j = 1 to 10

a(i, j) = int(rnd * 101)

print tab((j - 1) * 5 + 2); a(i, j);

next j

next i

print

x = a(1, 1)

for i = 1 to 10

for j = 1 to 10

if a(i, j) > x then m = i: n = j: x = a(i, j)

next j

next i

print "最大數為:" & x & ",它為第 " & m & "行," & "第" & n & "列的元素。"

end sub

執行介面

3.某陣列中有10個元素,元素的值由鍵盤輸入,要求將前5個元素與後5個元素對換。即第1個元素與第10個元素互換,第2個元素與第9個元素互換……。請輸出陣列中原來各元素的值和對換後各元素的值。

**option base 1

private sub command1_click()

dim a%(1 to 10)

dim i%

for i = 1 to 10

a(i) = inputbox("請輸入第" & i & "個數")

print a(i);

next i

print

for i = 1 to 5

t = a(i)

a(i) = a(10 - i + 1)

a(10 - i + 1) = t

print a(i);

next i

for i = 1 to 5

print a(i + 5);

next i

end sub

執行介面

4.輸入若干個學生的一門課的成績,統計各分數段的人數。按小於60分、60~69分、70~79分、80~89分、90~100分各為一個分數段。

**private sub command1_click()

dim a$(), k%, y

dim x(0 to 10) as integer

a = split(text1.text, ",")

for each y in a

k = y \ 10

x(k) = x(k) + 1

next y

picture1.print "統計結果如下"

picture1.print "90-100分有:" & x(9) + x(10) & "人"

for i = 8 to 6 step -1

picture1.print i * 10 & "分—"; i * 10 + 9 & "分有" & x(i) & "人"

next i

for i = 1 to 5

z = z + x(i)

next i

picture1.print "60分以下的有:" & z & "人"

end sub

執行介面

5.我國身份證號碼的第18位是由前17位通過公式計算出來的,請程式設計:要求從鍵盤上輸入其一身份證號碼的前17位,求出第18位

**private sub command1_click()

dim w(), a(0 to 16), s%, y%

for i = 0 to 16

w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)

a(i) = inputbox("請輸入第" & i + 1 & "個數")

print a(i);

s = s + a(i) * w(i)

next i

print

y = s mod 11

y = y + 1

m = choose(y, "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2")

print "第十八位是:" & m

end sub

執行介面

6.有一個數列,前兩項為1,從第三項開始,每一項都是前現兩項之和,輸出該數列前30項。

**option base 1

private sub command1_click()

dim a!(1 to 30)

a(1) = 1: a(2) = 2

print a(1); a(2)

for i = 3 to 30

a(i) = a(i - 1) + a(i - 2)

print a(i);

next i

end sub

執行介面

7.隨機產生15個不重複的a~z之間的大寫字母,存放在字元陣列中。

**private sub command1_click()

dim a(1 to 15)

for i = 1 to 15

a(i) = int(rnd * 26) + 65

next i

for i = 1 to 14

for j = i + 1 to 15

if a(j) = a(i) then a(j) = int(rnd * 26) + 65

next j

print chr(a(i));

next i

end sub

執行介面

8.將從鍵盤上輸入的一個數插入到陣列指定位置。

**private sub command1_click()

dim a(), l%, u%

dim x%, k%

a = array(1, 2, 3, 4, 5, 6, 7, 9, 10)

l = lbound(a)

u = ubound(a)

for i = l to u

print a(i);

next i

print

k = inputbox("請輸入要插入的位置")

x = inputbox("請輸入要插入的數")

redim preserve a(l to u + 1)

for i = u to k step -1

k = i

a(i + 1) = a(i)

next i

a(k) = x

for i = l to u + 1

print a(i);

next i

end sub

執行介面

9.在陣列中查詢從鍵盤上輸入的一個數,如果找不到輸出相應資訊,如果找到,則刪除該元素。

**private sub command1_click()

dim a(), p as boolean, x%, l%, u%, i%, k%

a = array(1, 5, 8, 9, 4, 6, 3, 7)

l = lbound(a): u = ubound(a)

for i = l to u

print a(i);

next i

print

x = val(inputbox("請輸入要刪除的數"))

for i = l to u

if x = a(i) then

p = true: k = i

exit for

end if

next i

if p = true then

for i = k + 1 to u

a(i - 1) = a(i)

next i

u = u - 1

redim preserve a(l to u)

for i = l to u

print a(i);

next i

else

msgbox "未找到!"

end if

end sub

執行介面

10.求一個m行n列的矩陣四周元素之和,元素值可以隨機產生,m、n從鍵盤輸入。

**private sub command1_click()

dim a%(), m%, n%, i%, j%, b%, c%, d%, e%, f%

m = inputbox("請輸入該矩陣的行數")

n = inputbox("請輸入該矩陣的列數")

redim a%(1 to m, 1 to n)

for i = 1 to m

for j = 1 to n

a(i, j) = int(rnd * 11)

print tab((j - 1) * 6 + 3); a(i, j);

next j

next i

for j = 1 to n

b = b + a(1, j)

e = e + a(m, j)

next j

for i = 2 to m - 1

c = c + a(i, 1)

d = d + a(i, n)

next i

f = b + c + d + e

print

print f

end sub

執行介面

11.某校召開運動會有10人蔘加男子100米短跑決賽,運動員號碼和成績如表5-3所示,試編制程式,按成績由高到低排序。

**執行介面

2樓:匿名使用者

'第一題:

private sub command1_click()

dim m%, n%, i%, j%, temp&, s&

m = val(inputbox("請輸入矩陣行數m=", , "3"))

n = val(inputbox("請輸入矩陣列數n=", , "4"))

s=0for i = 1 to m

for j = 1 to n

temp = int(rnd() * 10)

print temp,

if i = 1 or j = 1 or i = m or j = n then s = s + temp

next

print

next

print "四周元素之和是:" & s

end sub

'第二題

private sub command2_click()

dim m%, n%, i%, a%()

redim a(10)

print "原陣列為:"

for i = 1 to ubound(a)

a(i) = i: print a(i),

next

print

m = val(inputbox("請輸入插入的位置m=", , "3"))

n = val(inputbox("請輸入要插入的數n=", , "9999"))

redim preserve a(ubound(a) + 1)

for i = ubound(a) to m step -1

a(i) = a(i - 1)

next

a(m) = n

print "插入後陣列為:"

for i = 1 to ubound(a)

print a(i),

next

print

end sub

vb程式設計從鍵盤上輸入兩個正整數m和n求m和n的最

你明白這種求公因數演算法的思路就理解了。這是輾轉相除法 比如求 18 和 48 的最大公因數 第一部 大數除以小數取餘數 48 18 2 12第二部 餘數是零,結束運算,小數即為最大公因子 餘數不是零,繼續利用輾轉相除法,小數除以餘數再取餘數 18 12 1 6 第三步 如果餘數是零,則計算結束,上...

c語言輸入2行3列的矩陣A和3行4列的矩陣B,計算

解題過程如下 include include define l 2,define m 3,define n 4 printf n return int main void double b m n double c l n int i 0,j 0,k 0 printf ngenerate a d d...

vb程式設計 大一的 急求,VB程式設計 大一的 急求

你題目是1到100,每行輸出五個,而100裡只有35 和70能同時被5和7整除,所以我改了一下1000以內,如果要100以內,把1000改成100就可以了 private sub command1 click dim i as integer,n as integer n 0 for i 1 to ...