修改时间:2021-05-20 浏览次数:169 类型:开学考试
Const n=5
Dim a(1 to n) as integer, i as integer, j as integer, k as integer, t as integer
For i=1 to n
a(i)=int(rnd*20+1)
Next i
For i=1 to n-1
k=i
For j=i+1 to n
If a(j) mod 2=1 then
If a(k) mod 2=0 or a(j)>a(k) then k=j
ElseIf a(k) mod 2=0 and a(j)>a(k) then
k=j
End if
Next j
If k<>i then t=a(k): a(k)=a(i): a(i)=t
Next i
该程序段运行结束后,a(1)~a(5)可能是( )
Dim s1 As String, s2 As String, ch As String
Const code = "@*()^&#"
s1 = Text1.Text
For i = 1 To Len(s1)
ch = Mid(s1, i, 1)
If ch >= "a" And ch <= "z" Then
t = (Asc(ch) - 65 + i) Mod 7
s2 = Mid(code, t + 1, 1) + s2
Else
s2 = s2 + ch
End If
Next i
Text2.Text = s2
若 text1 中输入字符“AbCd”,则 text2 中输出的是 ( )
图 a
图 b
图 a
解题思路:随机生成 10 条船的载重量[1,10]和费用[1,30],然后将它们按费用从小到大排序,若费用相同,则载重大的在前,运输时,先把费用低的船装满,再按费用向下将船装好,运输。
图 a
Dim f(1 To 10) As Integer, w(1 To 10) As Integer, b(1 To 10) As Integer
Private Sub Command2_Click()
Dim n As Integer, count As Integer, yf As Integer
Dim s As String, i as integer
n = Val(Text1.Text)
count = 0
yf = 0
cz = n
i = 1
Do While n > 0
If n >= w(b(i)) Then
Else
yf = yf + n * f(b(i))
End If
count=count+1
s = s + Str(b(i)) + "号船"
i=i+1
Loop
Label2.Caption = "总计费用为:" + Str(yf) + "元 " + Str(count) + " 条船,分
别为 " + s
End Sub
Private Sub form_load()
Dim t As Integer, i as integer, j as integer
List1.Clear
List1.AddItem "编号 载重 费用'"
For i = 1 To 10
w(i) = Int(Rnd * 10 + 1)
f(i) = Int(Rnd * 30 + 1)
b(i) = i
List1.AddItem Str(b(i)) + " " + Str(w(i)) + " " + Str(f(i))
Next i
For i = 1 To 9
For j = 10 To i + 1 Step -1
If Then
t = b(j): b(j) = b(j - 1): b(j - 1) = t
End If
Next j
Next i
End Sub
解题思路:A矩阵放a数组,B矩阵放b数组,相乘后的结果放在d数组
矩阵相乘的方式为:
⒈用A的第1行各个数与B的第1列各个数对应相乘相加,得到的结果就是第1行第1列的数;
⒉用A的第1行各个数与B的第2列各个数对应相乘相加,得到的结果是第1行第2列的数;
……
⒋依次进行,(直到)用A的最末行各个数与B的第末列各个数对应相乘相加,结果是最末行第末列的数。
图 a
图 b
Dim a(1 To 9) As Integer, b(1 To 9) As Integer '矩阵 A,B 的数组
Private Sub Command1_Click()
Dim s As String, i As Integer, ch As String
Dim x As Integer, y As Integer, k As Integer
Dim d(1 To 9) As Integer '新矩阵的数组
s = Text1.Text 'B 矩阵以字符串的形式输入
k = 1
'输入 B 矩阵的值
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch = "0" Or ch = "1" Then
b(k) = Val(ch)
k = k + 1
If k = 10 Then Exit For
End If
Next i
'计算出新的矩阵的值
For i = 1 To 9
For j = 1 To 3
Next j
Next i
'输出新矩阵的值
For i = 1 To 3
ch = ""
For j = 1 To 3
ch = ch + Str(d((i - 1) * 3 + j)) + " "
Next j
List2.AddItem ch
Next i
Max = 1
For i = 2 To 9
If d(Max) < d(i) Then Max = i
Next i
x = ((Max - 1) \ 3 + 1)
y =
Label2.Caption = "最大的数是" + Str(d(Max)) + "位置(" + Str(x) + "," + Str(y) +
")"
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer, s As String
s = ""
Randomize
'生成矩阵 A 数组
For i = 1 To 9
a(i) = Int(Rnd * 20 + 1) * (-1) ^ (Int(Rnd * 2))
If Then
List1.AddItem s
s = ""
End If
s = s + " " + Str(a(i))
Next i
List1.AddItem s
End Sub
试题篮