Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Spiral-matrix/PowerShell/spiral-matrix.psh
Normal file
36
Task/Spiral-matrix/PowerShell/spiral-matrix.psh
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
function Spiral-Matrix ( [int]$N )
|
||||
{
|
||||
# Initialize variables
|
||||
$X = 0
|
||||
$Y = -1
|
||||
$i = 0
|
||||
$Sign = 1
|
||||
|
||||
# Intialize array
|
||||
$A = New-Object 'int[,]' $N, $N
|
||||
|
||||
# Set top row
|
||||
1..$N | ForEach { $Y += $Sign; $A[$X,$Y] = ++$i }
|
||||
|
||||
# For each remaining half spiral...
|
||||
ForEach ( $M in ($N-1)..1 )
|
||||
{
|
||||
# Set the vertical quarter spiral
|
||||
1..$M | ForEach { $X += $Sign; $A[$X,$Y] = ++$i }
|
||||
|
||||
# Curve the spiral
|
||||
$Sign = -$Sign
|
||||
|
||||
# Set the horizontal quarter spiral
|
||||
1..$M | ForEach { $Y += $Sign; $A[$X,$Y] = ++$i }
|
||||
}
|
||||
|
||||
# Convert the array to text output
|
||||
$Spiral = ForEach ( $X in 1..$N ) { ( 1..$N | ForEach { $A[($X-1),($_-1)] } ) -join "`t" }
|
||||
|
||||
return $Spiral
|
||||
}
|
||||
|
||||
Spiral-Matrix 5
|
||||
""
|
||||
Spiral-Matrix 7
|
||||
Loading…
Add table
Add a link
Reference in a new issue