Dim a(1 To 5)As Integer
a(1)=1 : a(2)=2
For i=3 To 5
a(i)=a(1)
For j=i-1 To 2 Step -1
a(j)=a(j)+a(j-1)
Next j
Next i
Text1.Text=Str(a(3))
i = 1 : k = 0
Do While i<= 3
For j = 8 To i + 1 Step -1
If a(j) >= a(j - 1) Then
t = a(j): a(j) = a(j-1): a(j-1) = t
k = k + 1
End If
Next j
i = i + 1
Loop
若数组a中的数据依次为“24,29,31,24,12,10,12,25”,则加工结束后,变量k的值为( )
图b
Private Sub Command1_Click()
Dim n, k, i ,sAs Integer
s = Val(Text1.Text) : k = 2
Do While n < s
For i = 1 To k \ 2
If k Mod i = 0 Then
If (m + i) Mod 2 = 0 And (m - i) Mod 2 = 0 Then
n = n + 1
Exit For
End If
End If
Next i
k = k + 1
Loop
Label2.Caption = "第" + Text1.Text + "个好数是" +
End Sub
图1
图2
Private Sub Command1_Click()
Dim n, i, j, temp As Integer
Dim a(1 To 10) As Integer
n = Val(Text1.Text)
Randomize
a(1) = Int(Rnd * 20) + 1
For i = 2 To n
a(i) = Int(Rnd * 20) + 1
If a(i) > a(i - 1) Then
temp = a(i)
bot = 1 : Top = n
Do While bot <= Top
‘(1)
If temp < a(m) Then bot = m + 1 Else Top = m - 1
Loop
For k = n To bot + 1 Step -1
‘(2)
Next k
a(bot) = temp
End If
Next i
For j = 1 To n
List1.AddItem Str(a(j))
Next j
End Sub
以上程序段运行时,为了实现上述功能,加框处代码应改正为:
⑴;⑵。
删数的算法如下:
⑴如果k>0,则从前往后检测相邻字符,否则,转⑶;
⑵①若所有相邻字符都已非降序,则将串尾k个字符删去,k值置0,转⑴;
②若相邻两数存在逆序(即前一个数>后一个数),则将前一个数删除,k值变化,然后回到⑴;
⑶去掉串首的0,输出结果。
按照上述算法思路,编写了VB程序,功能如下:在文本框Text1中输入数字串,在文本框Text2中输入删数的个数,单击“处理”按钮Command1,在文本框Text3中显示最小的整数。程序运行界面如图所示。
delete函数说明:delete(st,x,y)为自定义函数,功能为在字符串st中删除x位置开始的y长度的子串。
Private Sub Command1_Click()
Dim s As String, k As Integer, i As Integer, j As Integer, n As Integer
s = Text1.Text
k = Val(Text2.Text)
n = Len(s)
Do While k > 0
i = 1
Do While i < n And
i = i + 1
Loop
If i = n Then
n = n - k
k = 0
Else
s = delete(s, i, 1)
n = n - 1
End If
Loop
i = 1
Do While n > 1 And Mid(s, 1, 1) = "0"
s = delete(s, 1, 1)
i = i + 1
n = n - 1
Loop
Text3.Text = s
End Sub
Function delete(st As String, x As Integer, y As Integer) As String
delete = Mid(st, 1, x - 1) + Mid(st, x + y) 'mid函数第3个参数省略,则截取从开始位置向右到字符串结尾的所有字符
End Function