。 (2)請(qǐng)?jiān)冖佗冖厶幪钌虾线m代碼,實(shí)現(xiàn)程序功能。 Dim n As Integer Dim a(1 To 100)As Integer'a數(shù)組存儲(chǔ)各堆果子重量 Dim b(o To 100)As Integer'b(0)存放a數(shù)組中最小值的下標(biāo),若a(i)是數(shù)組中最大數(shù),則 b(i)的值為-1 Dim flag(1 To 100)As Boolean Private Sub Form_Load ____ 初始化果子的數(shù)量n和每堆果子的重量a(i),并依此顯示在列表框list1中,代碼略 初始化b數(shù)組的值均為-1,flag 數(shù)組的值均為False,代碼略 End Sub Private Sub Command1_Click _____ Dim i As Integer,k As Integer Dim p As Integer,q As Integer,w As Integer p=0 Do While True'生成b數(shù)組,標(biāo)記升序后a(i)的下一個(gè)元素在a數(shù)組中的位置為b(i) For i=1 To n If Not flag(i)Then k=i:Exit For Next i If i=n+1 Then Exit Do i=1 Do While i<=n If Not flag(i) And a(k)>a(i)Then k=i i=i+1 Loop b(p)=k flag(k)=True ①
p=b(p)或p=k
p=b(p)或p=k
Loop w=0 p=b(0):q=b(p) Do While b(p)<>-1 a(p)=a(p)+a(q) w=②
w+a(p)
w+a(p)
If b(q)<>-1 Then b(0)=b(q)Else Exit Do Call sort(p) p=b(0):q=b(p) Loop Text1.Text=Str(w) End Sub Sub sort(p As Integer)'將a(p)插入到合適位置 Dim pl As Integer,ql As Integer pl=b(0) If a(p)<=a(pl)Then b(0)=p:b(p)=pl Else Do While a(p)>a(p1)And b(p1)<>-1 ql=pl:pl=b(pl) Loop If b(p1)=-1 And a(p)>a(p1)Then b(pl)=p:b(p)=-1 Else b(p)=pl:③
1.大部分社交軟件都有好友推薦的功能,當(dāng)用戶 A 和用戶 B 的共同好友數(shù)量超過閾值 p 時(shí),由系統(tǒng)向用戶 A 推薦用戶 B。 編寫 VB 程序,實(shí)現(xiàn)好友推薦功能。運(yùn)行程序,列表框 Listl 中顯示用戶 id 及好友列表,在文本框 Textl 中 輸入推薦目標(biāo)用戶 id,在文本框 Text2 中輸入閾值 p,點(diǎn)擊“推薦”按鈕,在列表框List2 中顯示用戶之間的關(guān)系,在標(biāo)簽 Label5 中顯示向目標(biāo)用戶推薦的好友列表。程序運(yùn)行界面如圖: (1)根據(jù)如圖所示數(shù)據(jù),若輸入用戶 id 為“3”,輸入閾值為“3”,則推薦好友為: