1
AlloVince 2013-04-03 10:53:08 +08:00
用内置函数只需2行
function testArray(array $array){ sort($array); if(array_pop($array) == array_sum($array)) return true; return false; } echo testArray(array(4, 6, 23, 10, 3)); PS:你给出的例子是错的,[4, 6, 23, 10, 1, 3], 4+6+10+1+3 = 24 |
2
SonicXP 2013-04-03 10:55:41 +08:00
@AlloVince if any combination of numbers in the array can be added up to equal the largest number in the array
|
3
alexrezit 2013-04-03 10:58:42 +08:00 via iPad
只会穷举的算法弱菜灰过...
|
4
best1a 2013-04-03 11:01:13 +08:00
目测排序后DP?
算了,还是去做毕设吧。。。 |
5
xunyu 2013-04-03 11:02:39 +08:00
不要用排序,一次遍历求和,同时找到最大值,用和减去最大值判断是否等于最大值就可以了
|
6
200 2013-04-03 11:04:05 +08:00
背包 = =
|
7
hzlzh OP @AlloVince 你理解错误,我给的例子没错。没要求剩下数字都要用
如:true--[1, 22, 23, 25, 26] 注:25+1 = 26 |
8
66450146 2013-04-03 11:11:36 +08:00
去掉最大数之后就是很典型的 01 背包问题
|
9
lookhi 2013-04-03 11:13:37 +08:00
一场复杂的背包啊
我就选择跳过了 看楼上楼下的了 |
10
ftwbzhao 2013-04-03 11:31:02 +08:00
|
11
kilimanjaroup 2013-04-03 11:35:10 +08:00
@66450146 不是吧,01背包要求非负
|
12
zxc111 2013-04-03 11:40:30 +08:00
@kilimanjaroup 全加上最大负数的绝对值,结果作相应处理
|
13
AlloVince 2013-04-03 12:10:12 +08:00
|
14
limu 2013-04-03 13:05:36 +08:00
|
15
RisingV 2013-04-03 13:14:38 +08:00
穷举的危害是会有很多重复的子计算,只要在穷举基础上加上简单的防止重复子计算的措施就行了。
简单且高效。 |
16
laskuma 2013-04-03 13:26:09 +08:00
01背包+binary search优化
|
17
bhuztez 2013-04-03 13:36:06 +08:00
这分明就是Hello, world!级的Prolog题
$ swipl Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.0.2) Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- ['addition.pl']. % library(clpr) compiled into clpr 0.06 sec, 2,498 clauses % library(option) compiled into swi_option 0.00 sec, 32 clauses % pure_input compiled into pure_input 0.00 sec, 48 clauses % library(pio) compiled into pio 0.00 sec, 52 clauses % library(simplex) compiled into simplex 0.08 sec, 2,835 clauses % addition.pl compiled 0.08 sec, 2,867 clauses true. ?- once(solve([4, 6, 23, 10, 1, 3], _)). true. ?- once(solve([4, 6, 23, 10, 1, 3], R)). R = [{10, 1}, {6, 1}, {4, 1}, {3, 1}, {1, 0}]. ?- once(solve([5, 7, 16, 1, 2], _)). false. ?- once(solve([1, 22, 23, 24, 27, 29, 33], _)). false. ?- once(solve([1, 22, 23, 25, 26], _)). true. ?- once(solve([1, 22, 23, 25, 26], R)). R = [{25, 1}, {23, 0}, {22, 0}, {1, 1}]. ?- once(solve([3, 5, -1, 8, 1, -2, 12], _)). true. ?- once(solve([3, 5, -1, 8, 1, -2, 12], R)). R = [{8, 1}, {5, 1}, {3, 0}, {1, 0}, {-1, 1}, {-2, 0}]. ?- |
18
hzlzh OP |
19
hpyhacking 2013-04-04 11:51:05 +08:00
排序后剔除max,计算剩余sum
sum < max -> false sum = max -> true sum > max -> combos(sum).each do |c| sum(c) == max ? true : continue false |
20
hpyhacking 2013-04-04 11:54:50 +08:00
肯定还有其他更好的解法,数组长了上面的程序直接PASS掉。
|
21
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) |
22
breakind 2013-04-04 17:06:41 +08:00
错了一点,上面if maxValue == getSubArraySum(array,i):应该为if maxValue == getSubArraySum(tempArray,i):
|
23
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 |
24
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 没有优化 目测这个网站的大数据测试很水 |
27
monkeylyf 2013-04-05 11:38:51 +08:00
又看了下题目 这题含有负数 背包解不了 还是直接穷举吧
|