本帖最后由 DAIC 于 2013-10-22 10: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
http://blog.chinaunix.net/uid-9781829-id-1997947.html |