在寻找等价货币系统时,小美发现了如下规律:
1)与给定货币系统等价的货币系统必定是该货币系统的子集;
2)如果货币系统中的某个面额可以被其他货币表示时,该面额是无效的;
为此,小美按照如下方法构造最小等价货币系统B:先将原货币系统A的所有面额升序排序,每次把A中可以被B中的货币表示的面额删去后,将A中的最小面额放入B中。以此类推。基于此方法,小美编写了如下程序,在文本框Text1中输入给定的货币系统,单击按钮Command1后,在标签Label1中输出与其等价的货币系统的最小面额种数,在标签Label2中输出该货币系统。程序运行界面如图所示。
Private Sub Command1_Click()
Dim s As String, tmp As String, c As String
Dim n As Integer, i As Integer, j As Integer, ans As Integer
Dim a(1 To 100) As Integer, b(1 To 10000) As Boolean
'数组b(i)用于表示值i能否用已经放入新货币系统中的面额来表示
'此段程序用于将给定货币系统存储在a数组中并将其元素个数存储在变量n中
s = Text1.Text
tmp = "": n = 0
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c >= "0" And c <= "9" Then
ElseIf tmp <> "" Then
n = n + 1
a(n) = Val(tmp)
tmp = ""
End If
Next i
For i = 1 To n - 1
For j = n To i + 1 Step -1
If Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
End If
Next j
Next i
ans = 0: s = "{"
For i = 1 To a(n)
b(i) = False
Next i
For i = 1 To n
If Not b(a(i)) Then
ans = ans + 1
If ans <> 1 Then s = s + ","
s = s + CStr(a(i)) 'Cstr函数用于将数值变量转为字符串变量并去除首位空格
For j = a(i) + 1 To a(n)
If b(j - a(i))= True Then b(j) = True
Next j
End If
Next i
s = s + "}"
Label1.Caption = "与之等价的最小货币系统面额种数为" + Str(ans)
Label2.Caption = "其为" + s
End Sub