删数的算法如下:
⑴如果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