powershell 汉洛塔

#powershell 汉洛塔
#可变长数组Collections.ArrayList操作
#递归函数
#文字排版和配色
function hanoi($n)
{
$global:num=0
$global:arraya=New-Object Collections.ArrayList
$global:arrayb=New-Object Collections.ArrayList
$global:arrayc=New-Object Collections.ArrayList
$global:arraya.addrange(1..$n)
Write-Host $("初始状态:").PadLeft(13) -NoNewline
Write-Host ($global:arraya -join ",").PadRight(9) -NoNewline
Write-Host ($global:arrayb -join ",").PadRight(9) -NoNewline
Write-Host ($global:arrayc -join ",").PadRight(9)
function go($n,$a,$b,$c)
{ 
    if($n -eq 1)
        {   
        $global:num++
        if($a -eq "a" -and $c -eq "b"){$s1="a";$a1="-->     ";$s2="b  ";$a2="";$s3=""
        $global:arrayb.Insert(0,$global:arraya[0])
        $global:arraya.Removeat(0)
        }
        if($a -eq "a" -and $c -eq "c"){$s1="a";$a1="";$s2="---->";$a2="";$s3="c"
        $global:arrayc.Insert(0,$global:arraya[0])
        $global:arraya.Removeat(0)            
        }
        if($a -eq "b" -and $c -eq "a"){$s1="a";$a1="<--     ";$s2="b  ";$a2="";$s3=""
        $global:arraya.Insert(0,$global:arrayb[0])
        $global:arrayb.Removeat(0)            
        }
        if($a -eq "b" -and $c -eq "c"){$s1=" ";$a1="";$s2="b  ";$a2="-->";$s3="c"
        $global:arrayc.Insert(0,$global:arrayb[0])
        $global:arrayb.Removeat(0)            
        }
        if($a -eq "c" -and $c -eq "a"){$s1="a";$a1="";$s2="<----";$a2="";$s3="c"
        $global:arraya.Insert(0,$global:arrayc[0])
        $global:arrayc.Removeat(0)           
        }
        if($a -eq "c" -and $c -eq "b"){$s1=" ";$a1="";$s2="b  ";$a2="<--";$s3="c"
        $global:arrayb.Insert(0,$global:arrayc[0])
        $global:arrayc.Removeat(0)      
        }  
        Write-Host $("第$num`步:").PadLeft(15) -NoNewline
        Write-Host "$($s1)" -ForegroundColor 3 -NoNewline -BackgroundColor 15
        Write-Host "$($a1.PadLeft(17))" -ForegroundColor 1 -NoNewline -BackgroundColor 15
        Write-Host "$($s2.PadRight(12))" -ForegroundColor 9 -NoNewline -BackgroundColor 15
        Write-Host "$($a2.PadLeft(6))" -ForegroundColor 1 -NoNewline -BackgroundColor 15
        Write-Host "$($s3.PadLeft(12))" -ForegroundColor 12 -BackgroundColor 15
        Write-Host $("当前状态:").PadLeft(13) -NoNewline
        Write-Host ($global:arraya -join ",").PadRight(18) -NoNewline
        Write-Host ($global:arrayb -join ",").PadRight(12) -NoNewline
        Write-Host ($global:arrayc -join ",").PadLeft(18)
        }
    else
        {
        go ($n-1) $a $c $b
        go 1 $a $b $c
        go ($n-1) $b $a $c
        }

    }
go $n "a" "b" "c"
}

hanoi 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值