你好老大,我现在有一个想法,就是如果要排的是2个1、3个0那么对应的最大二进制数为11000最小为00011,将最大的11000转为十进制作递减,再转为二进制,然后作判断只保留含有两个1的字符串。
问题1:此方法可以吗?
问题2:我为此写了一段测试代码如下。但运行时for循环在第一次还是正确的,第二次11000所对应的十进制数一下变为-1,不明白为何会这样,还请老大指点
Private Sub Form_Click()
Dim i As Integer
Dim j As Integer
Dim n As String
Dim m As String
n = InputBox("输入字符串:")
m = StrReverse(n)
j = Con(n)
For i = Con(m) To j
Print De(j, Len(n))
j = j - 1
Next
End Sub
Public Function De(DecimalValue As Integer, MinimumDigits As Integer) As String '十进制转二进制
Dim result As String
Dim ExtraDigitsNeeded As Integer
DecimalValue = Abs(DecimalValue)
Do
result = CStr(DecimalValue Mod 2) & result
DecimalValue = DecimalValue \ 2
Loop While DecimalValue > 0
ExtraDigitsNeeded = MinimumDigits - Len(result)
If ExtraDigitsNeeded > 0 Then
result = String(ExtraDigitsNeeded, "0") & result
End If
De = result
End Function
Public Function Con(ByVal strBin As String) As Integer '二进制转十进制
Dim tmpVal As Integer
iCount
As Long
Dim tmpV
As String
For iCount = 1 To Len(strBin)
tmpVal = Val(tmpVal) + Val(Mid$(strBin, iCount, 1)) * (2 ^ (Len(strBin) - iCount))
Next iCount
Con = tmpVal
End Function