@test = [[6,7,8,5],[2,3,5],[5,7,9,8,7]]
def get_new_arg(arg)
@max = arg.size - 1
@max_h = arg.last.size
@arg = arg
@w = -1
@new_arg = Array.new
find_list([], 0)
return @new_arg
end
def find_list(list, index)
@h = 0
@test[index].each{|a|
if index == @max
@new_arg << (list + [a])
@h += 1
if @h == @max_h
return
end
else
l = (list + [a]).clone
find_list(l, index + 1)
end
}
end
p get_new_arg(@test)
以下引用美兽于2008-3-29 22:56:49的发言:
没测试,可能会有问题,但思路很清晰,回溯法。
@test = [[6,7,8,5],[2,3,5],[5,7,9,8,7]]
def get_new_arg(arg)
@max = @test.size - 1
@max_h = @test.last.size
@arg = arg
@w = -1
@new_arg = Array.new
find_list([], 0)
return @new_arg
end
def find_list(list, index)
@h = 0
@test[index].each{|a|
if index == @max
@new_arg << (list + [a])
@h += 1
if @h == @max_h
return
end
else
l = (list + [a]).clone
find_list(l, index + 1)
end
}
end
p get_new_arg(@test)
简单的测试下没问题,不过超级没看懂……
仔细研究研究
以下引用幻の飞鱼于2008-3-29 23:08:03的发言:
其实 << 的功能不是很懂
[本贴由作者于 2008-3-29 23:09:23 最后编辑]
以下引用幻の飞鱼于2008-3-29 23:19:35的发言:
<< 和直接用 = 的区别在哪??=。=
测试了几次没找出关键
@test = [[6,7,8,5],[2,3,5],[5,7,9,8,7]]
def get_new_arg(arg)
@arg = arg
@max = @arg.size - 1
@max_h = @arg.last.size - 1
@new_arg = Array.new
find_list([], 0)
return @new_arg
end
def find_list(list, index)
@h = 0
@arg[index].each{|a|
if index == @max
@new_arg << (list + [a])
@h += 1
return if @h > @max_h
end
find_list(list + [a], index + 1)
}
end
p get_new_arg(@test)
@test = [[6,7,8,5],[2,3,5],[5,7,9,8,7]]
def get_new_arg(arg)
@arg = arg
@max = @arg.size - 1
@new_arg = Array.new
find_list([], 0)
return @new_arg
end
def find_list(list, index)
@arg[index].each{|a|
if index == @max
@new_arg << (list + [a])
next
end
find_list(list + [a], index + 1)
}
end
p get_new_arg(@test)
欢迎光临 Project1 (https://rpg.blue/) | Powered by Discuz! X3.1 |