|
发表于 2010-6-21 16:15:04
|
显示全部楼层
[codes=jass]function DisplayExpressions takes string prefix, integer min, integer max, integer sum, boolean repeat returns nothing
if sum == 0 then
call DisplayTextToPlayer(Player(0), 0, 0, prefix)
return
endif
loop
exitwhen min > max or min > sum
if prefix != "" then
if repeat then
call DisplayExpressions(prefix + " + " + I2S(min), min, max, sum - min, repeat)
else
call DisplayExpressions(prefix + " + " + I2S(min), min + 1, max, sum - min, repeat)
endif
else
if repeat then
call DisplayExpressions(I2S(min), min, max, sum - min, repeat)
else
call DisplayExpressions(I2S(min), min + 1, max, sum - min, repeat)
endif
endif
set min = min + 1
endloop
endfunction[/codes]
参数prefix表示前缀,min为最小数,max为最大数,sum为需要的和,repeat为元素能否重复
使用递归实现,初始将prefix设为空字符串"",剩余参数按需设置
比如要得到用1-10,和为12,不允许重复的表达式,如下面这样调用即可
[codes=jass]call DisplayExpressions("", 1, 10, 12, false)[/codes] |
|