powershell 汉洛塔
2015-12-11 14:59
495 查看
#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
相关文章推荐
- shell根据系统硬盘大小删除文件
- shell if 多条件判断 时间大小比较
- shell之监控tomcat(一个tomcat)端口是否正常
- shell常用脚本
- shell脚本无法使用source的原因及解决方法
- python调用shell脚本
- shell调用脚本--直接退出不执行后续命令
- 5. storm常用命令(客户端storm shell)
- hive shell执行命令报错:Specified key was too long; max key length is 767 bytes
- 14.bash中 2>&1 & 的解释
- 13.工作命令汇集
- shell之逆向解析以生日生成的MD5
- 9.一些shell编程中命令的累积
- 8.shell中文本处理三剑客
- 7.shell分支与循环结构
- 4.shell变量替换技术
- 3.shell变量的子串应用
- 反弹SHELL汇总
- 2.shell变量的问题
- 1.bash解释+shell基础知识