更简单的算法。
Private Sub Command1_Click()
Dim x&, i%, j%, n%, p&, y$
Do
x = InputBox("请输入一个大于零的整数")
Loop Until x > 0
Do Until x = 0
n = Int(Log(x) / Log(2))
p = 2 ^ n
y = p & "+" & y
x = x - p
Loop
MsgBox Left(y, Len(y) - 1)
End Sub
rewrite :
Private Function QiuMi(ByVal MM As Long) As Long
Dim Re As Long
Dim I As Long
Dim J As Long
Dim K As Long
I = MM
K = Log(MM) / Log(2)
J = 2 ^ K
If J > I Then
K = K - 1
J = 2 ^ K
End If
Re = J
QiuMi = Re
End Function
Private Sub Command3_Click()
Dim I As Integer
Dim J As Integer
Dim m As Long
Dim n As Long
Dim shu As Long
shu = CLng(Text1.Text)
m = QiuJin(shu)
Print m
n = shu - m
Do While n <> 0
m = QiuMi(n)
Print m
n = n - m
Loop
End Sub
Private Function getbin(ByVal decnum As Long) As String
Dim i As Long
Dim j As Long
i = decnum
If i <= 0 Then
getbin = ""
Exit Function
End If
Dim k As Long, str As String
k = 0
str = ""
Do While j <= i
j = 2 ^ k
If (i And j) = j Then
str = "2^" + CStr(k) + "+" + str
End If
k = k + 1
Loop
str = Left(str, Len(str) - 1)
getbin = CStr(i) + "=" + str
End Function
Private Sub Command1_Click()
MsgBox getbin(511)
End Sub
Option Explicit
Ptivate FunctiOn TenturnTwo (ByVal varNum As Long)
Dim returnString As String,ModNum As Integer
DO While varNum>0
ModNum=varNum Mod 2
varNum=varNum\2
returnString=Trim(Str (ModNum))+returnString
Loop
TenturnTwo=returnString
End Function
Private Function TwoturnTen (ByVal varString As String)
Dim Slen As Long,I As Long,
returnNum As Long
Slen=Len(varString)
For I=o To Slen-1
returmNum=returnNum +Val(Mid (varString,I+1,1))*(2^(Slen-I-1))
Next
TWoturnTen=returnNum
End Function
Private Sub Command1_Click()
Dim x&, i%, j%, n%, p&, y$
x = InputBox("请输入一个整数")
n = Log(x) / Log(2)
y = ""
Do Until x = 0
p = 2 ^ n
If x - p >= 0 Then
y = p & "+" & y
x = x - p
End If
n = n - 1
Loop
MsgBox Left(y, Len(y) - 1)
End Sub
垃圾算法:如下:
Private Sub Command1_Click()
Dim m As Integer
Dim n As Integer
Dim shu As Integer
shu = CInt(Text1.Text)
m = QiuJin(shu)
If m <> -1 Then
n = shu - m
Print m
Else
Exit Sub
End If
Do While n <> 0
m = QiuJin(n)
If m <> -1 Then
n = n - m
Print m
Else
Exit Sub
End If
Loop
End Sub
Private Function QiuJin(ByVal QQ As Integer) As Integer
Dim Re As Integer
If QQ = 1 Then
Re = 1
ElseIf QQ >= 2 And QQ < 4 Then
Re = 2
ElseIf QQ >= 4 And QQ < 8 Then
Re = 4
ElseIf QQ >= 8 And QQ < 16 Then
Re = 8
ElseIf QQ >= 16 And QQ < 32 Then
Re = 16
ElseIf QQ >= 32 And QQ <= 64 Then
Re = 32
ElseIf QQ >= 64 And QQ < 128 Then
Re = 64
ElseIf QQ >= 128 And QQ < 256 Then
Re = 128
ElseIf QQ >= 256 And QQ < 512 Then
Re = 256
ElseIf QQ >= 512 And QQ < 1024 Then
Re = 512
ElseIf QQ >= 1024 And QQ < 2048 Then
Re = 1024
ElseIf QQ >= 2048 And QQ < 4096 Then
Re = 2048
ElseIf QQ >= 4096 And QQ < 8192 Then
Re = 4096
ElseIf QQ >= 8192 And QQ < 16384 Then
Re = 8192
ElseIf QQ >= 16384 And QQ < 32768 Then
Re = 16384
ElseIf QQ >= 32768 And QQ < 65536 Then
Re = 32768
Else
Re = -1
End If
QiuJin = Re
Private Sub Command1_Click()
MsgBox crack(100000)
End Sub
Function crack(ByVal num As Long) As String
Dim dectobin As String, temp As Long, i As Long
dectobin = ""
temp = num
Do While Not temp = 1
dectobin = temp Mod 2 & dectobin
temp = temp \ 2
Loop
dectobin = "1" & dectobin
For i = 1 To Len(dectobin)
If Mid(dectobin, i, 1) = "1" Then crack = crack & " 2^" & (Len(dectobin) - i) & " +"
Next
crack = IIf(Right(crack, 1) = "+", Left(crack, Len(crack) - 1), crack)
crack = num & " = " & crack
End Function