Dim a(1 To 26)As Integer
s=Text1. Text
For i=1 To Len(s)
①
If c>="A" And c<="Z" Then c=Chr(Asc(c)+32)
②
Next i
For i=1 To 26
If a(i)<> 0 Then Label1.Caption= Label1.Caption+ ③
Next i
Dim a(1 To 10) As Integer
s = "7218634594": n = Len(s)
t = 0
For i = 1 To n - 1
a(i) = Val(Mid(s, i, 2))
Next i
For i = 1 To n - 2 Step 2
k = i
For j = i + 2 To n - 1 Step 2
If a(j) < a(k) Then k = j
Next j
If k <> i Then
temp = a(i): a(i) = a(k): a(k) = temp: t = t + 1
End If
Next i
Text1.Text = Str(t)
该程序段运行后,文本框Text1中显示的内容是( )
Function delStr (S As String, m As Integer) As String
Dim n As Integer:n = Len(S)
If m <1 Or m > n Then delStr = "输入位置有误!"
If m = 1 Then delStr = Mid(S, 2, n-1)
If m < n Then delStr = Mid(S, 1, m-1)+Mid(S, m+1, n-m)
If m = n Then delStr = Mid(S, 1, n-1)
End Function
Private Sub Command1_ Click()
Dim S As String:Dim i As Integer
S = Text1.Text
For i = 1 To 10 Step 2
S = delStr(S, i)
Next i
Text2. Text = S
End Sub
文本框Text1中输入Happybirthday,执行该程序段,文本框Text2中显示的是( )
i = 1: L = 0: R = n + 1
Do While i < R
If a(i) < k1 Then
L = L + 1
If L <> i Then tt = a(i): a(i) = a(L): a(L) = tt
i = i + 1
ElseIf a(i) > k2 Then
R = R - 1
tt = a(i): a(i) = a(R): a(R) = tt
Else
i = i + 1
End If
Loop
则执行上述代码后,下列说法正确的是( )
请分析任务,完成下列题目。
程序代码如下:
Private Sub Command1_Click()
Dim s As single
Dim Max , Min ,i As Integer
Dim a(5) As Integer
Max = 0
Min = ①
s = 0
For i = 1 To 5
a(i) = InputBox("请输入第" & i & "个分数")
s = s + a(i)
If ② Then Max = a(i)
If Min > a(i) Then Min = a(i)
Next i
s = ③
Text1.Text = Max
Text2.Text = Min
Text3.Text = s
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Command3_Click()
End Sub
请分析任务,完成下列题目。
实现上述功能的VB代码如下,但加框处有错,请改正。
Private Sub Command1_Click()
Dim n As Integer,m As Integer
Dim a(1 To 50)As Integer
n= Val(Text1. Text)
m= Val(Text2. Text)
For i= 1 To n
Next i
s=0;j=0
Do While s <n
t=0
Do While t <m
t=t+ a(j)
Loop
a(j)=0
s=s+1
List1. Additem"第"+Str(s)+"次:"+Str(j)
Loop
End sub
应改为
请回答下列问题:
Private Sub Count_Click()
Dim word(1 To 500) As String, num(1 To 500) As Integer
Dim s As String, c As String, tmp As String, t As Integer
Dim i As Integer, j As Integer, k As Integer, low As Integer, high As Integer
Dim m As Integer, n As Integer, b As Integer
For i = 1 To 500
num(i) = 0
Next i
s = Text1.Text : n = Len(s)
i = 1 : b = 1 : m = 0 : j = 0
Do While i <= n
c = Mid(s, i, 1)
If c >= "A" And c <= "Z" Or c >= "a" And c <= "z" Then
j = j + 1
Else
If j > 0 Then
tmp =
low = 1
high = m
Do While low <= high
t = (low + high) \ 2
If tmp = word(t) Then
Exit Do
If tmp < word(t) Then
high = t - 1
Else
low = t + 1
Loop
If Then
For k = m To low Step -1
word(k + 1) = word(k)
num(k + 1) = num(k)
Next k
num(low) = 1
m = m + 1
Else
num(t) = num(t) + 1
End If
j = 0
End If
b = i + 1
End If
i = i + 1
Loop
List1.Clear
For i = 1 To m
List1.AddItem word(i) + ":" + Str(num(i)) + "次"
Next i
End Sub
Function Lowcase(t As String) As String
Dim c As String, s As String, i As Integer
s = ""
For i = 1 To Len(t)
c = Mid(t, i, 1)
If c >= "A" And c <= "Z" Then s = s + Chr(Asc(c) + 32) Else s = s + c
Next i
End Function
1)将每个人看成是一个钩码,能挂在另一个挂钩底下,自己底下也能挂其他挂钩。
2)如果两个钩码在同一条钩码串上,则为亲戚(如2、3都能找到1)。
3)如果两个是亲戚但不在一串钩码上,则可将其中一串的最上面的钩码挂在另一串的下方。
例如从图a加框处的前三行数据可得到图b所示的钩码串1和钩码串2,从第四行数据可知9、5也为亲戚,则可将两串钩码合并,合并后3为2的顶端,5为3的顶端,其余数据以此类推。
根据算法思想,编写VB程序功能如下:程序初始化时给出6条亲戚关系显示在List1中,单击“理关系”按钮,在List2中显示每个人所在钩码的顶端编号。在Text1和Text2中分别输入两人的编号,单击“询问”按钮,输出两个人是否为亲戚关系。程序运行界面如图a所示。
Const n = 9 '总人数
Const m = 6 '已知的亲戚关系数
Dim a(1 To n) As String , b(1 To n) As String
Dim f(1 To n) As String '记录不同的亲戚钩码串成员,每条亲戚钩码串内互为亲戚
Dim k As Integer '亲戚钩码串的数目
Private Sub Form_Load()
'初始化List1和List2,生成m条亲戚关系,每条亲戚关系的编号对应存储在数组a(i)和b(i) '中,在List1中输出a(i)和b(i),代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, x As Integer, y As Integer
f(1) = a(1) + b(1) '利用a(1)、b(1)生成第一条钩码串存储在f(1)中
k = 1 'k为钩码串编号
For i = 2 To m
x = root(a(i), 0): y = root(b(i), 0) 'x为调用函数获取a(i)所在的钩码串编号
If x = 0 And y = 0 Then k = k + 1: '根据x与y分类讨论
If x <> 0 And y = 0 Then f(x) = f(x) + b(i)
If x = 0 And y <> 0 Then f(y) = f(y) + a(i)
If x <> y And x <> 0 And y <> 0 Then 'x、y在不同钩码串则将两条钩码串合并
f(x) = f(x) + f(y): f(y) = f(k): k = k - 1
End If
Next i
For i = 1 To n '输出每个人所在钩码的顶端编号
x = root(CStr(i), 0): y = root(CStr(i), 1) 'Cstr()函数用于去除字符前导空格
If x <> 0 Then
If y = Len(f(x)) Then List2.AddItem Str(i) + " " + CStr(i)
If y <> Len(f(x)) Then List2.AddItem Str(i) + " " +
End If
Next i
End Sub
'按钮Command2的功能为判断输入的两个人是否为亲戚关系,代码略
Function root(a As String, b As Integer) As Integer '参数a表示待查找目标,参数b
'表示返回值类型,0表示返回目标所在钩码串编号,1表示返回目标在该钩码串中的位置
Dim m As Integer, n As Integer, i As Integer, j As Integer
m = 0: n = 0: i = k: j = 0
Do While i > 0
If a = Mid(f(i), Len(f(i)) - j, 1) Then m = i: n = Len(f(i)) - j: Exit Do
j = j + 1
If j = Len(f(i)) Then i = i - 1: j = 0
Loop
If Then root = m Else root = n
End Function
算法分析:要使得到的5位数字最小,首先需保证能取到5个数字,且首位应尽量小,那么取首位数的范围应保证余下有4个数字,即在前4位中选最小非0的数字。
程序运行时,在文本框Text1中输入一个数字字符串,在文本框Text2中输入要移除的数字个数,单击“运行”按钮CmdRun后,在标签Labell显示得到的最小新数,程序运行界面如图b所示。
Private Sub CmdRun_ Click()
Dim Num As String,New_num As String ' Num为输入的数字字符串
Dim i As Integer, j As Integer, m As Integer,
Dim As Integer, s As Integer, k As Integer
Dim a(30) As String, b(30) As Integer
Num = Text1. Text: s = Len(Num)
k = Val (Text2. Text) 'k为删除数字个数
For i=1 To s
a(i) = Mid(Num,i,1)
Nexti
j =1
m=1
n=
For i=m+l To n '取第一个非零最小数字
If Then m=I '加框处程序代码有误,请改正
Next i
b(j)= m
Do While '取其他最小数字
m=m+1
n=n+1
For i=m+1 To n
If a(i) < a(m) Then m= i
Next i
j =j+1:b(j)=m
Loop
For i= m+1 To s '最后直接取的数字
j =j+1:b(j)=i
Next i
For j=1 To s- k '输出最小数
New_ num =
Next j
Labell. Caption = New_num
End Sub