16.數(shù)字黑洞數(shù)6174.一個任意的四位正整數(shù)(四位數(shù)完全相同除外),將數(shù)字重新組合成一個最大的數(shù)和最小的數(shù)相減,重復(fù)這個過程,最多七步,必得6174.如取四位數(shù)5462,按以上方法作運(yùn)算如下: ①6542-2456=4086 ②8640-0468=8172 ③8721-1278=7443 ④7443-3447=3996 ⑤9963-3699=6264⑥6642-2466=4176 ⑦7641-1467=6174 從文本框Text1中輸入一個任意的數(shù)字不完全相同的四位正整數(shù),在文本框Text2中輸出掉進(jìn)黑洞的步數(shù).實(shí)現(xiàn)上述功能的VB代碼如下,但加框處代碼有錯,請改正: ①
② Private Sub Command1-Click( ?。?br />Dim a(1 TO4)As Integer,c As Integer,tAs In teger Dim x As Integer,yAs Integer,iAs Integer,j AsInteger x=Val(Text1.Text) c=0 Do While x<>6174 For i=l To4'把四位正整數(shù)x的每一位數(shù)字取出來并存放到數(shù)組a中 a(i)=xMod10 '① Next i Fori=1To3 Forj=i+lTo4 IfThen'② t=a(i):a(i)=a(j):a(j)=t end Next j Next i x=a(1)*1000+a(2)^100+a(3)*10+a(4) y=a(1)+a(2)*10+a(3)*100+a(4)*1000 x=x-y c=c+1 Loop Text2.Text=Str(c) End Sub.
。 (2)實(shí)現(xiàn)上述功能的VB程序如下,請?jiān)跈M線處填入合適代碼。 Private Sub Command1_Click ( ) Dim i As Integer,S As String Dim str As String,ch As String str=Text1.Text List1.Clear For i=1 To Len(str) ch= ① S=convert(ch) List1.AddItem ch &“=>“& S Next i End Sub Private Function convert(ch As String) As String Dim m As Integer,k As Integer,n As Integer,i As Integer convert=““ n=Asc(ch) Do While n>0 ② convert=m & convert If m=1 Then k=k+1 End If n=n\2 Loop For i=1 To 7-Len(convert)'將字符的二進(jìn)制代碼補(bǔ)足7位 convert=“0“& convert Next i If k Mod 2=0 Then convert= ③ Else convert=“0“& convert End If End Function 程序中橫線處的代碼應(yīng)為