请高手 帮助 用c 或 VB 编写 一 凑数问题 具体问题如下…………
请高手 帮助 用c 或 VB 编写 一 凑数问题 具体问题如下…………编写一程序,从键盘输入数字R,计算机自动检查在下列算式的()中能否天上“+”或“—”凑成相应的等式。如能凑成,则打印出这些算式;如凑不成,则给出信息“NO answer!”1()2()3()4()5()6()7()8()9=R
Option Explicit Private Type 临时结果结构 算式 As String 结果 As Long End Type Dim 缓存(0 To 255) As 临时结果结构 Dim 缓存计数 As Long Private Sub Command1_Click() Dim i As Long i = Val(Text1.Text) '输入值 Dim o As Long o = 计算(i) '计算 If o = -1 Then '如果计算失败 Text2.Text = "NO answer!" '提示 Else '否则显示表达式及结果 Text2.Text = 缓存(o).算式 & "=" & 缓存(o).结果 End If End Sub Private Function 计算(cs As Long) As Long Dim i As Long For i = 0 To 缓存计数 '查找缓存,减少计算 If 缓存(i).结果 = cs Then '在缓存中找到, 计算 = i '那么直接返回结果 Exit Function End If Next i Dim j As Long Dim k As String, m As String Dim o As Long For i = 缓存计数 To 255 '计算,一共有 8 个符号位,每位用 1位为代替, 1 为加, 0 为减 k = HextoBin(CByte(i)) '转化为二进制,用来取位 m = "1" '第一个数 o = 1 For j = 2 To 9 '第二个数 到第 9 个数 If Mid(k, j - 1, 1) = 1 Then '根据符号位进行计算,组合表达式 m = m & "+" & j o = o + j Else m = m & "-" & j o = o - j End If Next j 缓存(i).算式 = m '如果计算结果与参数相符,那么停止计算,输出结果 缓存(i).结果 = o 缓存计数 = i '保存缓存的最大值,可以减少计算次数 If o = cs Then 计算 = i '返回结果 Exit Function End If Next i 缓存计数 = i '没找到结果,保存缓存 计算 = -1 '返回没结果 End Function Private Function HextoBin(cs As Byte) As String 'BYTE数值转 二进制字符串 Dim i As Long, j As String Dim m As String j = Hex(cs) If Len(j) = 1 Then j = "0" & j For i = 1 To 2 Select Case Mid(j, i, 1) Case "0" m = m & "0000" Case "1" m = m & "0001" Case "2" m = m & "0010" Case "3" m = m & "0011" Case "4" m = m & "0100" Case "5" m = m & "0101" Case "6" m = m & "0110" Case "7" m = m & "0111" Case "8" m = m & "1000" Case "9" m = m & "1001" Case "A", "a" m = m & "1010" Case "B", "b" m = m & "1011" Case "C", "c" m = m & "1100" Case "D", "d" m = m & "1101" Case "E", "e" m = m & "1110" Case "F" m = m & "1111" End Select Next i HextoBin = m End Function