本帖最后由 3mile 于 2010-8-1 14:21 编辑
我很笨,别笑我。#include <array.au3>
;$str = "912345678"
Local $time=TimerInit()
$str="913245678"
$len=StringLen($str)
Local $arr_n[$len+1][2], $temp[$len+1]
For $n = 1 To $len
$arr_n[$n][0] = $n
$arr_n[$n][1] = StringMid($str, $n, 1)
Next
aa()
_ArrayDisplay($arr_n,TimerDiff($time))
Func aa()
For $i = $len To 1 Step -1
If $arr_n[$i][0] = $arr_n[$i][1] Then
$temp[$i] = 1
ContinueLoop
Else
$temp[$i] = 0
EndIf
Next
;_ArrayDisplay($temp)
$numswap = _ArrayFindAll($temp, '0')
If @error Then Return
If UBound($numswap) > 1 Then
$sw1 = $numswap[UBound($numswap) - 1]
$sw2 = $numswap[UBound($numswap) - 2]
_ArraySwap($arr_n[$sw1][1], $arr_n[$sw2][1])
aa()
EndIf
EndFunc ;==>aa
没注意到要输出步骤,上面的仅作运行时间参考。
下面的有输出步骤:#include <array.au3>
$str = "912345678"
Local $time=TimerInit()
;$str="913245678"
$len=StringLen($str)
Local $arr_n[$len+1][2], $temp[$len+1],$output[10],$tempstr
Local $z=0
For $n = 1 To $len
$arr_n[$n][0] = $n
$arr_n[$n][1] = StringMid($str, $n, 1)
Next
aa()
_ArrayDisplay($arr_n,TimerDiff($time))
_ArrayDisplay($output,"输出步骤")
Func aa()
For $i = $len To 1 Step -1
If $arr_n[$i][0] = $arr_n[$i][1] Then
$temp[$i] = 1
ContinueLoop
Else
$temp[$i] = 0
EndIf
Next
;_ArrayDisplay($temp)
$numswap = _ArrayFindAll($temp, '0')
If @error Then Return
If UBound($numswap) > 1 Then
$sw1 = $numswap[UBound($numswap) - 1]
$sw2 = $numswap[UBound($numswap) - 2]
_ArraySwap($arr_n[$sw1][1], $arr_n[$sw2][1])
$tempstr=''
For $y=1 To 9
$tempstr&=$arr_n[$y][1]
Next
$output[$z]=$tempstr
$z+=1
aa()
EndIf
EndFunc ;==>aa
|