- 论坛徽章:
- 5
|
本帖最后由 starwing83 于 2012-03-31 11:45 编辑
回复 73# pandaiam - local function dfs(t, idx)
- if t.equals == t.result then error "ok" end
- local idx = idx or 1
- local cur = t[idx]
- if cur == t.len then
- t.equals = t.equals + 1
- dfs(t, idx + 1)
- t.equals = t.equals - 1
- return
- end
- for i = #t, idx+1, -1 do
- local res = cur + t[i]
- if res <= t.len then
- t[i] = res
- dfs(t, idx + 1)
- t[i] = t[i] - cur
- end
- end
- end
- local function solve(t)
- -- find the posible length
- table.sort(t)
- t.max = t[#t]
- t.sum = 0
- for i, v in ipairs(t) do t.sum = t.sum + v end
- for i = t.max, t.sum do
- t.result = t.sum/i
- if t.result == math.floor(t.result) then
- t.equals = 0
- t.len = i
- if not pcall(dfs,t) then return i end
- end
- end
- end
- print(solve {5,2,1,5,2,1,5,2,1,6})
- print(solve {2,3,5,7,11,13,17,19,23})
- print(solve {1,2,3,4})
复制代码 |
|