[一道有趣的算法题] 各种语言都来试试吧~

2013-04-03 10:40:12 +08:00
 hzlzh
## 目的
没什么目的,就是好玩,最近大伙在做一道有趣的算法题,拿来大家分享一下,有兴趣的可以试试。

## 题目描述:
Have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.

## 也就是说:
如:[4, 6, 23, 10, 1, 3] 这个数组(数字不重复,可以为负数)中选出最大的数字是 23
4+6+10+3 = 23
然后剩余的任意个数字数相加之和若等于23,则 true,否则false

## 测试用例:
输出--输入
false--[5, 7, 16, 1, 2]
false--[1, 22, 23, 24, 27, 29, 33]
true--[1, 22, 23, 25, 26] 注:25+1 = 26
true--[3, 5, -1, 8, 1, -2, 12] 注:5+(-1)+8 = 12

## 要求:
书写语言不限,不能是思路或伪代码,用如: php,python,js...等都可以在网站里运行检查

## 出处:
题目来源:
http://coderbyte.com/
时间要求:当然是越快越好

enjoy!

ps: 写出来的童鞋可以 [只贴Gist ID,如:/xxxx/123456] ,你懂得,别影响了别人思路。
7071 次点击
所在节点    程序员
30 条回复
breakind
2013-04-04 17:01:14 +08:00
这道题实质上就是求一个数组所有的子集,下面用python实现一个,使用的二进制取位来列举所有的子集
test1 = [5, 7, 16, 1, 2]
test2 = [1, 22, 23, 24, 27, 29, 33]
test3 = [1, 22, 23, 25, 26]
test4 = [3, 5, -1, 8, 1, -2, 12]

def getSubArraySum(array,index):
arrayLength = len(array)
sum = 0
for i in range(arrayLength):
if (index>>(arrayLength - i - 1))&1 == 1:
sum += array[i]
return sum
def checkArray(array):
tempArray = array
maxValue = max(tempArray)
del tempArray[tempArray.index(maxValue)]
istrue = False
for i in range(2**len(tempArray)):
if maxValue == getSubArraySum(array,i):
istrue = True
return istrue
return istrue

print checkArray(test1)
print checkArray(test2)
print checkArray(test3)
print checkArray(test4)
breakind
2013-04-04 17:06:41 +08:00
错了一点,上面if maxValue == getSubArraySum(array,i):应该为if maxValue == getSubArraySum(tempArray,i):
skywalker
2013-04-05 09:43:39 +08:00
Haskell:

import Data.List

chkArr :: Int -> [Int] -> Bool
chkArr amount arr
| amount == 0 = True
| null arr || amount < 0 = False
| otherwise = chkArr (amount-(head arr)) (tail arr) || chkArr amount (tail arr)

testArr :: [Int] -> Bool
testArr xs = chkArr (head ys) (tail ys)
where ys = sortBy (flip compare) xs
monkeylyf
2013-04-05 09:45:42 +08:00
https://gist.github.com/monkeylyf/5315975

”if any combination of numbers in the array can be added up to equal the largest number in the array“ 我对题目的理解是任意组合加起来等于最大的那个, 没说任意组合里不可以有最大的这个数
所以有一个test case 说我的结果不正确:[10,12,500,1,-5,1,0]
私以为, 所以存在组合0 + 500 = 500. 应该return true

思路是0/1 knapsack 没有优化 目测这个网站的大数据测试很水
skywalker
2013-04-05 11:07:57 +08:00
@monkeylyf 如果可以包含最大那个元素, 那肯定都要返回true了, 因为最大元素自己肯定等于自己.
monkeylyf
2013-04-05 11:27:54 +08:00
@skywalker 有道理. 题目可以写的再清晰点.
monkeylyf
2013-04-05 11:38:51 +08:00
又看了下题目 这题含有负数 背包解不了 还是直接穷举吧
hzlzh
2013-04-05 12:37:07 +08:00
@monkeylyf 用递归做一个最优递归。
lehui99
2013-04-05 15:22:29 +08:00
@xunyu 不要用排序,一次遍历求和,同时找到最大值,用和减去最大值判断是否等于最大值就可以了
+1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1
hzlzh
2013-04-05 15:24:14 +08:00
@xunyu
@lehui99 建议仔细看下题目和用例。
true--[1, 22, 23, 25, 26] 注:25+1 = 26

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/64824

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX