16.數(shù)字黑洞數(shù)6174.一個(gè)任意的四位正整數(shù)(四位數(shù)完全相同除外),將數(shù)字重新組合成一個(gè)最大的數(shù)和最小的數(shù)相減,重復(fù)這個(gè)過程,最多七步,必得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中輸入一個(gè)任意的數(shù)字不完全相同的四位正整數(shù),在文本框Text2中輸出掉進(jìn)黑洞的步數(shù).實(shí)現(xiàn)上述功能的VB代碼如下,但加框處代碼有錯(cuò),請(qǐng)改正: ①
② Private Sub Command1-Click( ) 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.
. Private Sub Command1-Click( ?。?br />Dim a( ?。〢s Integer,b( )AsInteger,m As Integer,n As Integer Dim i As Integer,k As Integer,cAs Integer,x As Integer n=Val(Textl.Text):m=Val(Text2.Text):x=Val(Text3.Text) ReDim a(lTon)As Lnteger'重新定義數(shù)組a,存儲(chǔ)下標(biāo)對(duì)應(yīng)編號(hào)人的隊(duì)列狀態(tài) ReDim b(lTon)As Lnteger'重新定義數(shù)組b,存儲(chǔ)下標(biāo)對(duì)應(yīng)編號(hào)人的密碼 Randomize'隨機(jī)數(shù)生成器初始化 For i=l To n a(i)=l'在列
'產(chǎn)生每個(gè)人的隨機(jī)密碼 Next c=0 k=0 Listl.Clear Do While c<n For i=l To n k=k+a(i)'累計(jì)報(bào)數(shù) Ifk=mAnd
Then k=0 a(i)=0'出列 Listl.Addltem''出列人的編號(hào)為''&i&'',該人的密碼為''&b(i)