设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4177|回复: 3
打印 上一主题 下一主题

编程题目(分割铅块)

[复制链接]
跳转到指定楼层
1#
发表于 2005-11-1 22:44:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有一40千克的铅块,把它分成4块(每块为整数重量),用这4块铅块作为天平称的砝码,可以称出1----40千克任何重量(整数重量)的物体,问:如何分割?

注意:

1.天平两边都可以放砝码。

2.以上所有的数值均为整数,而不是小数。

3.一次性称出重量,而不能分步称出,然后求其总和。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

2#
发表于 2005-11-2 08:54:00 | 只看该作者
我有些晕
3#
发表于 2005-11-2 20:14:00 | 只看该作者
Public Sub Test()

Dim a As Integer, b As Integer, c As Integer, d As Integer, i As Integer

For a = 1 To 40: For b = a To 40: For c = b To 40: For d = c To 40

  If a + b + c + d = 40 Then

    For i = 1 To 39

        If CheckNum(i, a, b, c, d) = False Then Exit For

    Next

    If i = 40 Then Debug.Print a & "," & b & "," & c & "," & d

  End If

Next: Next: Next: Next

End SubPublic Function CheckNum(Num As Integer, w As Integer, x As Integer, y As Integer, z As Integer) As Boolean

For a = -1 To 1: For b = -1 To 1: For c = -1 To 1: For d = -1 To 1

  If a * w + b * x + c * y + d * z = Num Then

    CheckNum = True

    Exit Function

  End If

Next: Next: Next: Next

End Function

答案是:1,3,9,27(跟我用心算的结果一样,并且是唯一解 :)

4#
 楼主| 发表于 2005-11-2 21:57:00 | 只看该作者
可以简化一下a肯定等于1,d 肯定等于 40-1-b-cPublic Sub Test()

Dim b As Integer, c As Integer, i As Integer

For b = 1 To 37

   For c = b To 37

      For i = 1 To 39

         If CheckNum(i, b, c) = False Then Exit For

      Next

      If i = 40 Then Debug.Print "1," & b & "," & c & "," & 40 - 1 - b - c

   Next

Next

End SubPublic Function CheckNum(Num As Integer, x As Integer, y As Integer) As Boolean

For a = -1 To 1

   For b = -1 To 1

      For c = -1 To 1

         For d = -1 To 1

            If a + b * x + c * y + d * (40 - 1 - x - y) = Num Then

               CheckNum = True

               Exit Function

            End If

         Next

      Next

   Next

Next

End Function
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-1 09:29 , Processed in 0.104773 second(s), 28 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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