learn321
发表于 2011-9-18 12:18:19
本帖最后由 learn321 于 2011-9-18 12:30 编辑
我也试着做了一个。可郁闷的很,有输出,可结果和老大们对不上,看了半天也不知道哪儿错了{:face (229):}#include<array.au3>
Global $frist, $second, $third, $i = 0, $outarray
Dim $sum, $sum1, $sum2, $sum3, $sum_average, $i_t
;读入数据
For $t = 0 To 429
$shuju_line = StringRegExp(FileReadLine(@ScriptDir & "\数据.txt", $t + 1), '[.\d]+', 3)
$frist[$t] = Number($shuju_line)
$second[$t] = Number($shuju_line)
$third[$t] = Number($shuju_line)
Next
While 1
_Mysum()
_Mytiaozheng($frist, $second, $sum1 - $sum_average)
_Mysum()
_Mytiaozheng($frist, $third, $sum1 - $sum_average)
_Mysum()
_Mytiaozheng($second, $third, $sum2 - $sum_average)
If $i_t = $i Then ExitLoop
$i_t = $i
WEnd
MsgBox(0, "", "共计调整" & $i & "次!")
_Myout()
_ArrayDisplay($outarray)
;3组数组和及平均值
Func _Mysum()
$sum1 = 0
$sum2 = 0
$sum3 = 0
$sum_average = 0
Local $t
For $t = 0 To 429
$sum1 += $frist[$t]
$sum2 += $second[$t]
$sum3 += $third[$t]
Next
$sum_average = ($sum1 + $sum2 + $sum3) / 3
EndFunc ;==>_Mysum
;按差值查看数组是否需要调整
Func _Mytiaozheng(ByRef $temp1, ByRef $temp2, $temp_diff)
Local $m, $n, $temp_chan, $temp_min = , $temp_t
For $m = 0 To 429
For $n = 0 To 429
If $temp_diff > 0 And $temp1[$m] > $temp2[$n] Then
$temp_chan = $temp_diff - $temp1[$m] + $temp2[$n]
ElseIf $temp_diff < 0 And $temp1[$m] < $temp2[$n] Then
$temp_chan = $temp_diff - $temp1[$m] + $temp2[$n]
EndIf
If Abs($temp_chan) < $temp_min Then
$temp_min = Abs($temp_chan)
$temp_min = $m
$temp_min = $n
EndIf
Next
Next
;数据交换
If $temp_min < $temp_diff Then
$temp_t = $temp1[$temp_min]
$temp1[$temp_min] = $temp2[$temp_min]
$temp2[$temp_min] = $temp_t
$i += 1
EndIf
EndFunc ;==>_Mytiaozheng
;数组输出
Func _Myout()
Local $m
$outarray = $sum1
$outarray = $sum2
$outarray = $sum3
For $m = 1 To 430
$outarray[$m] = $frist[$m - 1]
$outarray[$m] = $second[$m - 1]
$outarray[$m] = $third[$m - 1]
Next
EndFunc ;==>_Myout
binghc
发表于 2011-9-18 13:00:16
本帖最后由 binghc 于 2011-9-18 13:01 编辑
回复 62# learn321
从客户角度看,没有什么对不对的只要你原来数字不改它,不把原来的1变成2,最后最结果相差不是很大的话,那都ok,都是对的
但是从程序员角度看,这个算法就纯在瑕疵,从你发上来的图片看。我觉得你的第二列和第三列之间的数据肯定还可以互换,从这方面找找是不是这样的
doraegeep
发表于 2011-9-18 21:15:58
完全凌乱了看不懂咯
飞越星海
发表于 2011-9-18 21:56:29
不明白!!!!!!!!!!!!这是实话!!!
learn321
发表于 2011-9-19 08:36:08
本帖最后由 learn321 于 2011-9-19 12:06 编辑
呵呵,又做了一个,重新调整算法,数据调整了4次,结果完全ok!哦也!!!#include<array.au3>
Global $frist, $second, $third, $i = 0, $outarray, $sum_average, $fir_cha, $sec_cha, $thi_cha, $sum1, $sum2, $sum3
Dim $i_t
;读入数据
For $t = 0 To 429
$shuju_line = StringRegExp(FileReadLine(@ScriptDir & "\数据.txt", $t + 1), '[.\d]+', 3)
$frist[$t] = Number($shuju_line)
$second[$t] = Number($shuju_line)
$third[$t] = Number($shuju_line)
Next
_Mysum()
$sum_average = ($sum1 + $sum2 + $sum3) / 3
While 1
_Mycha()
If $fir_cha = 0 Then
_Mytiaozheng($second, $third, $sec_cha)
ElseIf $sec_cha = 0 Then
_Mytiaozheng($frist, $third, $fir_cha)
ElseIf $thi_cha = 0 Then
_Mytiaozheng($frist, $second, $fir_cha)
ElseIf ($fir_cha > 0 And $second > 0) Or ($fir_cha < 0 And $sec_cha < 0) Then
If Abs($fir_cha) > Abs($sec_cha) Then
_Mytiaozheng($frist, $third, $fir_cha)
Else
_Mytiaozheng($second, $third, $sec_cha)
EndIf
ElseIf ($fir_cha > 0 And $thi_cha > 0) Or ($fir_cha < 0 And $thi_cha < 0) Then
If Abs($fir_cha) > Abs($thi_cha) Then
_Mytiaozheng($frist, $second, $fir_cha)
Else
_Mytiaozheng($third, $second, $thi_cha)
EndIf
ElseIf Abs($sec_cha) > Abs($thi_cha) Then
_Mytiaozheng($second, $frist, $sec_cha)
Else
_Mytiaozheng($third, $frist, $thi_cha)
EndIf
_Mysum()
If $i_t = $i Then ExitLoop
$i_t = $i
WEnd
MsgBox(0, "", "共计调整" & $i & "次!")
_Myout()
_ArrayDisplay($outarray)
;3组数组和及平均值
Func _Mysum()
$sum1 = 0
$sum2 = 0
$sum3 = 0
Local $t
For $t = 0 To 429
$sum1 += $frist[$t]
$sum2 += $second[$t]
$sum3 += $third[$t]
Next
EndFunc ;==>_Mysum
;按差值查看数组是否需要调整
Func _Mytiaozheng(ByRef $temp1, ByRef $temp2, $temp_diff)
Local $m, $n, $temp_change, $temp_min = , $temp_t
For $m = 0 To 429
For $n = 0 To 429
$temp_change = Abs(($temp_diff) - $temp1[$m] + $temp2[$n])
If $temp_change < $temp_min Then
$temp_min = $temp_change
$temp_min = $m
$temp_min = $n
EndIf
Next
Next
;数据交换
If $temp_min < Abs($temp_diff) Then
$temp_t = $temp1[$temp_min]
$temp1[$temp_min] = $temp2[$temp_min]
$temp2[$temp_min] = $temp_t
$i += 1
EndIf
EndFunc ;==>_Mytiaozheng
;数组输出
Func _Myout()
Local $m
$outarray = $sum1
$outarray = $sum2
$outarray = $sum3
For $m = 1 To 430
$outarray[$m] = $frist[$m - 1]
$outarray[$m] = $second[$m - 1]
$outarray[$m] = $third[$m - 1]
Next
EndFunc ;==>_Myout
;判读差值
Func _Mycha()
$fir_cha = $sum1 - $sum_average
$sec_cha = $sum2 - $sum_average
$thi_cha = $sum3 - $sum_average
EndFunc ;==>_Mycha
结果如下图:
又思考了一下,主程序段算法还可以优化,if太多,代码太乱,自己读都困难,头真大啊。。。
chie530520
发表于 2011-9-21 09:15:03
感覺好負雜哦...不過有挑戰性 做做看好囉
805333
发表于 2011-10-9 22:45:29
这个算法不错
menfan1
发表于 2011-10-12 20:14:40
看看啥内容