嗯,没有找到好的算法.
给出个笨办法,也算为如此好的题目做下抛砖引玉的烂砖头吧,希望能引来数学高手.
#include <array.au3>
Local $arr[18] = [1, 2, 3, 3, 2, 4, 5, 3, 4, 6, 4, 5, 7, 5, 6, 8, 6, 7]
$e=_ArrayToString($arr,"+")
Local $array[UBound($arr) / 3][3], $sum
For $i = 0 To UBound($array) - 1
Local $temp[3] = [Number($arr[$i * 3]), Number($arr[$i * 3 + 1]), Number($arr[$i * 3 + 2])]
$array[$i][0] = $temp[0]
$array[$i][1] = $temp[1]
$array[$i][2] = $temp[2]
Next
_ArrayDisplay($array,"原始数据")
MsgBox(0, "总和", _sum($array, 0) & @CRLF & _sum($array, 1) & @CRLF & _sum($array, 2))
$AVG = Int(Execute($e)/3)
For $i = 0 To UBound($array) - 1
$max = max($array[$i][0], $array[$i][1], $array[$i][2])
$min = min($array[$i][0], $array[$i][1], $array[$i][2])
$sum0 = _sum($array, 0)
$sum1 = _sum($array, 1)
$sum2 = _sum($array, 2)
$max_sum = max($sum0, $sum1, $sum2)
$min_sum = min($sum0, $sum1, $sum2)
If $sum0 = $sum1 And $sum1 = $sum2 Then ExitLoop
If Eval("sum"&$max_sum) > $AVG Then
If Number($array[$i][$max_sum]) <= Number($array[$i][$min_sum]) Then ContinueLoop
$temp = $array[$i][$max_sum]
$array[$i][$max_sum] = $array[$i][$min_sum]
$array[$i][$min_sum] = $temp
EndIf
Next
_ArrayDisplay($array,"结果")
MsgBox(0, "结果&每列总和", _sum($array, 0) & @CRLF & _sum($array, 1) & @CRLF & _sum($array, 2))
Func _sum($array, $col)
If UBound($array, 0) <> 2 Then Return SetError(0)
Local $sum = 0
For $i = 0 To UBound($array) - 1
$sum += $array[$i][$col]
Next
Return $sum
EndFunc ;==>_sum
Func Max($x, $y, $z)
$x = Number($x)
$y = Number($y)
$z = Number($z)
Local $index = 0;[2]=[0,$x]
If $y > $x Then
$index = 2
If $y > $z Then
$index = 1
EndIf
EndIf
If $z > $x Then
$index = 1
If $z > $y Then
$index = 2
EndIf
EndIf
Return $index
EndFunc ;==>Max
Func min($x, $y, $z)
$x = Number($x)
$y = Number($y)
$z = Number($z)
Local $index = 0;[2]=[0,$x]
If $y < $x Then
$index = 2
If $y < $z Then
$index = 1
EndIf
EndIf
If $z < $x Then
$index = 1
If $z < $y Then
$index = 2
EndIf
EndIf
Return $index
EndFunc ;==>min
|