Dim i as Integer, max as Integer Dim a(1 to 10) as Integer
‘代码略,随机产生 10 个不重复的数存于数组 a 的 10 个元素 a(1)~a(10)中max=a(1)
For i=2 to 10
If a(i)>a(max) then max=i Next i
变量 max 用于存储( )
图 a
图b
图 c
Dim s As String, st As Long, ch As Integer
Dim i As Integer, n As Integer
n = Len(s)
st = 0
For i = 1 To n
ch =
st = st + ch * 2 ^ (i - 1)
next i
Text2.Text = Str(st) End Sub
长度小于等于 100),在列表框 List1 里输出所有配对括号在表达式中的位置。例如在文本框 Text1 里入:“12*(3-(1+2))*3”,程序运行效果如图所示。
实现上述功能的 VB 代码如下,在划线的地方填入合适代码,完善程序。Private Sub Command1_Click()
Dim s As String
Dim a(1 To 100) As Integer
|
i = 1 top = 0
Do While i <= Len(s)
If Mid(s, i, 1) = "(" Then
top = top + 1
End If
If Mid(s, i, 1) = ")" Then
List1.AddItem Str(a(top)) + " " + Str(i)
End If
i=i+1 Loop
End Sub
小张编写 VB 程序,实现如下功能:在前 100 个偶自然数中,计算恰好有四位为 1 的二进制数的个数, 如图所示,单击“计算”按钮 Command1,在文本框 Text1 中显示符合条件的个数,在列表框 List1 中显示符合条件的二进制数。VB 程序代码如下:
提示:本题“除 2 取余倒排法”的方法将十进制转化为二进制,算法如下: 例如十进制数“97”转化为二进制的过程如右图所示:
因此“97”二进制值为:1100001
Dim a As Integer, c As Integer, w As Integer, i As Integer
Dim s As String, k As String
c = 0
For m = 1 To 100
k = ""
i = 2 * m
Do While i > 0
a=i Mod 2
k = str(a) + k
Loop
For n = 1 To Len(k)
s = Mid(k, n, 1)
If s = "1" Then w = w + 1
Next n
If w = 4 Then
c = c + 1
End if
Next m
Text1.Text = Str(c) End Sub