设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: lzx-shmily
打印 上一主题 下一主题

求最簡單代碼

[复制链接]
11#
发表于 2005-6-13 21:38:00 | 只看该作者
贴一个我自己做的,肯定不是最优。Sub abc(a1 As Double, a2 As Double, a3 As Double, a4 As Double, a5 As Double, a6 As Double, a0 As Double)

Dim i As Byte, j As Byte, k As Byte, l As Byte, m As Byte, n As Byte

Dim gshi As String, zhi As Double, oldzhi As Doubleoldzhi = a0

For i = 0 To 1

    For j = 0 To 1

        For k = 0 To 1

            For l = 0 To 1

                For m = 0 To 1

                    For n = 0 To 1

                        ai = a1: If i = 0 Then ai = 0

                        aj = a2: If j = 0 Then aj = 0

                        ak = a3: If k = 0 Then ak = 0

                        al = a4: If l = 0 Then al = 0

                        am = a5: If m = 0 Then am = 0

                        an = a6: If n = 0 Then an = 0

                        zhi = Abs(ai + aj + ak + al + am + an - a0)

                        If zhi < oldzhi Then

                           oldzhi = zhi

                           gshi = ai & "+" & aj & "+" & ak & "+" & al & "+" & am & "+" & an & " = " & ai + aj + ak + al + am + an

                        End If

                    Next n

                Next m

            Next l

        Next k

    Next j

Next i

gshi = Replace(gshi, "+0+", "+", , 4)

gshi = Replace(gshi, "+0=", "=")

If Left(gshi, 2) = "0+" Then gshi = Right(gshi, Len(gshi) - 2)

MsgBox gshi

End Sub

调用:abc 3.1, 1.7, 2, 5.3, 0.9, 7.2, 10     '6个计算数及1个接近数
12#
发表于 2005-6-15 07:52:00 | 只看该作者
Public Sub PL()

Dim i As Integer, Total As Single, tmp As Single, Str As String, strTmp As String

    For i = 1 To 2 ^ 6 - 1

        strTmp = ((i And 2 ^ 5) / 2 ^ 5) * 3.1 & "+" & ((i And 2 ^ 4) / 2 ^ 4) * 1.7 & "+" & ((i And 2 ^ 3) / 2 ^ 3) * 2# & "+" & ((i And 2 ^ 2) / 2 ^ 2) * 5.3 & "+" & ((i And 2 ^ 1) / 2 ^ 1) * 0.9 & "+" & ((i And 2 ^ 0) / 2 ^ 0) * 7.2

        tmp = Abs(Eval(strTmp) - 10)

        If tmp < Total Or Total = 0 Then

            Total = tmp

            Str = strTmp

        ElseIf tmp = Total Then

            Str = Str & " or " & strTmp

        End If

    Next

    MsgBox Str & "=" & Total

End Sub

确实想不出更短的了。答案不是两组,而是三组:0+0+2+0+.9+7.2 or 0+1.7+2+5.3+.9+0 or 3.1+1.7+0+5.3+0+0=.1
13#
发表于 2005-6-15 15:35:00 | 只看该作者
高手
14#
发表于 2005-6-15 19:35:00 | 只看该作者
高!!,非一般思维,IQ要达120以上了[em05]
15#
发表于 2005-6-15 21:16:00 | 只看该作者
由以上想到ublic Sub GetNumber()

Dim I, J, K, L, M, N As Integer, Total As Single, str As String, tmp As Double

For I = 0 To 1: For J = 0 To 1: For K = 0 To 1: For L = 0 To 1: For M = 0 To 1: For N = 0 To 1

strTmp = I * 3.1 & "+" & J * 1.7 & "+" & K * 2# & "+" & L * 5.3 & "+" & M * 0.9 & "+" & N * 7.2

tmp = Abs(Eval(strTmp) - 10)

If tmp < Total Or Total = 0 Then

  Total = tmp

     str = strTmp

    ElseIf tmp = Total Then

  str = str & " or " & strTmp

End If

Next N: Next M: Next L: Next K: Next J: Next I

MsgBox str & "=" & Total

End Sub[em05][em05][em05]
16#
发表于 2005-6-15 21:37:00 | 只看该作者
如果只要求一组解,下面应该是最短的了:去掉Tmp中间变量,就剩一个循环和一条语句。Public Function Str(Optional i As Integer, Optional Tmp As String) As String

    For i = 1 To 2 ^ 6 - 1

        Tmp = (i And 32) / 32 * 3.1 & "+" & (i And 16) / 16 * 1.7 & "+" & (i And 8) / 8 * 2

        Tmp = Tmp & "+" & (i And 4) / 4 * 5.3 & "+" & (i And 2) / 2 * 0.9 & "+" & (i And 1) * 7.2

        Str = IIf((Abs(Eval(Tmp) - 10) < Abs(Eval(IIf(Str = "", "0", Str)) - 10)), Tmp, Str)

    Next

End Function下面是两个自定义获取和设置一个整数某一位的位操作函数,在使用一个整数保存多个开关量的时候可以用到,如用一个字段保存多个选项。另外使用此函数的话,上面的程序语句可以短一些:Public Function GetByte(Num As Long, i As Integer) As Long

    GetByte = (Num And 2 ^ i) / 2 ^ i

End FunctionPublic Function SetByte(Num As Long, i As Integer, val As Integer) As Long

    SetByte = IIf(val = 1, Num Or 2 ^ i, Num - (Num And 2 ^ i))

End Function

[建议把此帖转到技术竞赛栏目]

点击这里给我发消息

17#
发表于 2005-6-17 07:01:00 | 只看该作者

求最簡單代碼

非常不错, 我现转到竞赛栏目
18#
发表于 2005-8-3 18:53:00 | 只看该作者
以下是引用Trynew在2005-6-15 13:37:00的发言:



如果只要求一组解,下面应该是最短的了:去掉Tmp中间变量,就剩一个循环和一条语句。

Public Function Str(Optional i As Integer, Optional Tmp As String) As String

    For i = 1 To 2 ^ 6 - 1

        Tmp = (i And 32) / 32 * 3.1 & "+" & (i And 16) / 16 * 1.7 & "+" & (i And 8) / 8 * 2

        Tmp = Tmp & "+" & (i And 4) / 4 * 5.3 & "+" & (i And 2) / 2 * 0.9 & "+" & (i And 1) * 7.2

        Str = IIf((Abs(Eval(Tmp) - 10) < Abs(Eval(IIf(Str = "", "0", Str)) - 10)), Tmp, Str)

    Next

End Function

下面是两个自定义获取和设置一个整数某一位的位操作函数,在使用一个整数保存多个开关量的时候可以用到,如用一个字段保存多个选项。另外使用此函数的话,上面的程序语句可以短一些:

Public Function GetByte(Num As Long, i As Integer) As Long

    GetByte = (Num And 2 ^ i) / 2 ^ i

End Function

Public Function SetByte(Num As Long, i As Integer, val As Integer) As Long

    SetByte = IIf(val = 1, Num Or 2 ^ i, Num - (Num And 2 ^ i))

End Function



[建议把此帖转到技术竞赛栏目]

这个方法确实是最简单的,递归的代码长度和执行效率都是很不错的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-16 01:36 , Processed in 0.083620 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表