本帖最后由 foboy 于 2010-8-29 10:49 编辑
这个函数可以满足你的需求,算出来,每个数之间的差可能是3或者4,不是一个等差数列,结果输出的顺序是从大到小。
例子中是求3个和为16的数。
如果要求11个和为41的数, 可以运行:test(41,11)
不过这样算是没有结果的。
test(16, 3)
Func test($sum, $num_num)
For $_i = 0 To $sum
If $_i * $num_num + $num_num * ($num_num - 1) * 1.5 > $sum Then ExitLoop
$_result = check($_i, $sum, $num_num)
If Not @error Then
ConsoleWrite($_result & @CRLF & $_i & @CRLF & "**********"&@CRLF)
EndIf
Next
EndFunc ;==>test
Func check($now_num, $sum, $num_count, $now_sum = -1, $now_count = 1)
If $now_sum == -1 Then $now_sum = $now_num
$now_num += 3
If $now_num > $sum Then Return SetError(1, 0, "")
$now_count += 1
$now_sum += $now_num
If $now_sum == $sum And $now_count == $num_count Then Return $now_num
$_result = check($now_num, $sum, $num_count, $now_sum, $now_count)
If Not @error Then
ConsoleWrite($_result & @CRLF)
Return $now_num
EndIf
If $now_sum + 1 == $sum And $now_count == $num_count Then Return $now_num + 1
$_result = check($now_num + 1, $sum, $num_count, $now_sum + 1, $now_count)
If Not @error Then
ConsoleWrite($_result & @CRLF)
Return $now_num + 1
EndIf
Return SetError(1, 0, "")
EndFunc ;==>check
|