全部博文(74)
分类: WINDOWS
2008-09-15 21:25:19
题目很简单, 如图所示:
PS C:\> s-x 1
x
PS C:\> s-x 2
x x
x
x x
PS C:\> s-x 3
x x x x
x x
x x x x
x x
x
x x
x x x x
x x
x x x x
当n = 1时, 输出一个"x", 如果n = 2的话, 就输出
x x
x
x x
以此类推. 下面附上利用递归下降方式编写的PowerShell版本:
function vo($vx = '', $hx = '', $cur, $total) {
if ($cur -eq 1) {
if ($vx -eq $hx) {
Write-Host -No "x";
}
else {
Write-Host -No " ";
}
return;
}
for ($i = 1; $i -le 3; $i++) {
$t = $hx
vo ($vx) ([string]$t + ($i % 2)) ($cur - 1) $total
}
}
function ho($vx = 1, $hx = 1, $cur, $total) {
if ($cur -eq 1) {
for ($i = 1; $i -le 3; $i++) {
vo ($vx) ($hx) $total $total
""
return;
}
}
for ($i = 1; $i -le 3; $i++) {
$t = $vx
ho ([string]$t + ($i % 2)) ($hx) ($cur - 1) $total
}
}
function s-x($i = 3) {
if ($i -gt 10) { Write-Warning "Too large"; return }
if ($i -lt 1) { Write-Warning "Must be a positive number"; return }
ho -cur $i -total $i
}
下面给出n = 4的结果:
PS C:\> s-x 4
x x x x x x x x
x x x x
x x x x x x x x
x x x x
x x
x x x x
x x x x x x x x
x x x x
x x x x x x x x
x x x x
x x
x x x x
x x
x
x x
x x x x
x x
x x x x
x x x x x x x x
x x x x
x x x x x x x x
x x x x
x x
x x x x
x x x x x x x x
x x x x
x x x x x x x x
dobear_09222010-05-04 20:26:40
http://blog.chinaunix.net/u3/114344/showart.php?id=2227295 这个是我写的,也是递归算法,效率稍微要高一点。