Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -99,7 +99,7 @@ LOOPJ CR R5,R12
|
|||
LA R5,1(R5)
|
||||
B LOOPJ
|
||||
ENDLOOPJ EQU *
|
||||
WTO MF=(E,WTOMSG)
|
||||
WTO MF=(E,WTOMSG)
|
||||
LA R4,1(R4)
|
||||
B LOOPI
|
||||
ENDLOOPI EQU *
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
-- Spiral Square
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
|
||||
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
|
||||
|
||||
procedure Spiral_Square is
|
||||
type Array_Type is array(Positive range <>, Positive range <>) of Natural;
|
||||
|
||||
function Spiral (N : Positive) return Array_Type is
|
||||
Result : Array_Type(1..N, 1..N);
|
||||
Row : Natural := 1;
|
||||
Col : Natural := 1;
|
||||
Max_Row : Natural := N;
|
||||
Max_Col : Natural := N;
|
||||
Min_Row : Natural := 1;
|
||||
Min_Col : Natural := 1;
|
||||
begin
|
||||
for I in 0..N**2 - 1 loop
|
||||
Result(Row, Col) := I;
|
||||
if Row = Min_Row then
|
||||
Col := Col + 1;
|
||||
if Col > Max_Col then
|
||||
Col := Max_Col;
|
||||
Row := Row + 1;
|
||||
end if;
|
||||
elsif Col = Max_Col then
|
||||
Row := Row + 1;
|
||||
if Row > Max_Row then
|
||||
Row := Max_Row;
|
||||
Col := Col - 1;
|
||||
end if;
|
||||
elsif Row = Max_Row then
|
||||
Col := Col - 1;
|
||||
if Col < Min_Col then
|
||||
Col := Min_Col;
|
||||
Row := Row - 1;
|
||||
end if;
|
||||
elsif Col = Min_Col then
|
||||
Row := Row - 1;
|
||||
if Row = Min_Row then -- Reduce spiral
|
||||
Min_Row := Min_Row + 1;
|
||||
Max_Row := Max_Row - 1;
|
||||
Row := Min_Row;
|
||||
Min_Col := Min_Col + 1;
|
||||
Max_Col := Max_Col - 1;
|
||||
Col := Min_Col;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
return Result;
|
||||
end Spiral;
|
||||
|
||||
procedure Print(Item : Array_Type) is
|
||||
Num_Digits : constant Float := Log(X => Float(Item'Length(1)**2), Base => 10.0);
|
||||
Spacing : constant Positive := Integer(Num_Digits) + 2;
|
||||
begin
|
||||
for I in Item'range(1) loop
|
||||
for J in Item'range(2) loop
|
||||
Put(Item => Item(I,J), Width => Spacing);
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Print;
|
||||
|
||||
begin
|
||||
Print(Spiral(5));
|
||||
end Spiral_Square;
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
function Spiral (N : Positive) return Array_Type is
|
||||
Result : Array_Type (1..N, 1..N);
|
||||
Left : Positive := 1;
|
||||
Right : Positive := N;
|
||||
Top : Positive := 1;
|
||||
Bottom : Positive := N;
|
||||
Index : Natural := 0;
|
||||
begin
|
||||
while Left < Right loop
|
||||
for I in Left..Right - 1 loop
|
||||
Result (Top, I) := Index;
|
||||
Index := Index + 1;
|
||||
end loop;
|
||||
for J in Top..Bottom - 1 loop
|
||||
Result (J, Right) := Index;
|
||||
Index := Index + 1;
|
||||
end loop;
|
||||
for I in reverse Left + 1..Right loop
|
||||
Result (Bottom, I) := Index;
|
||||
Index := Index + 1;
|
||||
end loop;
|
||||
for J in reverse Top + 1..Bottom loop
|
||||
Result (J, Left) := Index;
|
||||
Index := Index + 1;
|
||||
end loop;
|
||||
Left := Left + 1;
|
||||
Right := Right - 1;
|
||||
Top := Top + 1;
|
||||
Bottom := Bottom - 1;
|
||||
end loop;
|
||||
Result (Top, Left) := Index;
|
||||
return Result;
|
||||
end Spiral;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
spiralMatrix: function [n][
|
||||
m: new array.of: @[n,n] null
|
||||
m: array.of: @[n,n] null
|
||||
|
||||
[dx, dy, x, y]: [1, 0, 0, 0]
|
||||
|
||||
|
|
@ -8,12 +8,11 @@ spiralMatrix: function [n][
|
|||
|
||||
[nx,ny]: @[x+dx, y+dy]
|
||||
|
||||
if? and? [and? [in? nx 0..n-1][in? ny 0..n-1]][
|
||||
switch and? [and? [nx <=> 0 n-1][ny <=> 0 n-1]][
|
||||
null? m\[ny]\[nx]
|
||||
][
|
||||
[x,y]: @[nx, ny]
|
||||
]
|
||||
else [
|
||||
][
|
||||
bdx: dx
|
||||
[dx, dy]: @[neg dy, bdx]
|
||||
[x, y]: @[x+dx, y+dy]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
#include <vector>
|
||||
#include <memory> // for auto_ptr
|
||||
#include <cmath> // for the ceil and log10 and floor functions
|
||||
#include <memory> // for auto_ptr
|
||||
#include <cmath> // for the ceil and log10 and floor functions
|
||||
#include <iostream>
|
||||
#include <iomanip> // for the setw function
|
||||
#include <iomanip> // for the setw function
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
@ -11,57 +11,57 @@ typedef vector< IntRow > IntTable;
|
|||
|
||||
auto_ptr< IntTable > getSpiralArray( int dimension )
|
||||
{
|
||||
auto_ptr< IntTable > spiralArrayPtr( new IntTable(
|
||||
dimension, IntRow( dimension ) ) );
|
||||
auto_ptr< IntTable > spiralArrayPtr( new IntTable(
|
||||
dimension, IntRow( dimension ) ) );
|
||||
|
||||
int numConcentricSquares = static_cast< int >( ceil(
|
||||
static_cast< double >( dimension ) / 2.0 ) );
|
||||
int numConcentricSquares = static_cast< int >( ceil(
|
||||
static_cast< double >( dimension ) / 2.0 ) );
|
||||
|
||||
int j;
|
||||
int sideLen = dimension;
|
||||
int currNum = 0;
|
||||
int j;
|
||||
int sideLen = dimension;
|
||||
int currNum = 0;
|
||||
|
||||
for ( int i = 0; i < numConcentricSquares; i++ )
|
||||
{
|
||||
// do top side
|
||||
for ( j = 0; j < sideLen; j++ )
|
||||
( *spiralArrayPtr )[ i ][ i + j ] = currNum++;
|
||||
for ( int i = 0; i < numConcentricSquares; i++ )
|
||||
{
|
||||
// do top side
|
||||
for ( j = 0; j < sideLen; j++ )
|
||||
( *spiralArrayPtr )[ i ][ i + j ] = currNum++;
|
||||
|
||||
// do right side
|
||||
for ( j = 1; j < sideLen; j++ )
|
||||
( *spiralArrayPtr )[ i + j ][ dimension - 1 - i ] = currNum++;
|
||||
// do right side
|
||||
for ( j = 1; j < sideLen; j++ )
|
||||
( *spiralArrayPtr )[ i + j ][ dimension - 1 - i ] = currNum++;
|
||||
|
||||
// do bottom side
|
||||
for ( j = sideLen - 2; j > -1; j-- )
|
||||
( *spiralArrayPtr )[ dimension - 1 - i ][ i + j ] = currNum++;
|
||||
// do bottom side
|
||||
for ( j = sideLen - 2; j > -1; j-- )
|
||||
( *spiralArrayPtr )[ dimension - 1 - i ][ i + j ] = currNum++;
|
||||
|
||||
// do left side
|
||||
for ( j = sideLen - 2; j > 0; j-- )
|
||||
( *spiralArrayPtr )[ i + j ][ i ] = currNum++;
|
||||
// do left side
|
||||
for ( j = sideLen - 2; j > 0; j-- )
|
||||
( *spiralArrayPtr )[ i + j ][ i ] = currNum++;
|
||||
|
||||
sideLen -= 2;
|
||||
}
|
||||
sideLen -= 2;
|
||||
}
|
||||
|
||||
return spiralArrayPtr;
|
||||
return spiralArrayPtr;
|
||||
}
|
||||
|
||||
void printSpiralArray( const auto_ptr< IntTable >& spiralArrayPtr )
|
||||
{
|
||||
size_t dimension = spiralArrayPtr->size();
|
||||
size_t dimension = spiralArrayPtr->size();
|
||||
|
||||
int fieldWidth = static_cast< int >( floor( log10(
|
||||
static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;
|
||||
int fieldWidth = static_cast< int >( floor( log10(
|
||||
static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;
|
||||
|
||||
size_t col;
|
||||
for ( size_t row = 0; row < dimension; row++ )
|
||||
{
|
||||
for ( col = 0; col < dimension; col++ )
|
||||
cout << setw( fieldWidth ) << ( *spiralArrayPtr )[ row ][ col ];
|
||||
cout << endl;
|
||||
}
|
||||
size_t col;
|
||||
for ( size_t row = 0; row < dimension; row++ )
|
||||
{
|
||||
for ( col = 0; col < dimension; col++ )
|
||||
cout << setw( fieldWidth ) << ( *spiralArrayPtr )[ row ][ col ];
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printSpiralArray( getSpiralArray( 5 ) );
|
||||
printSpiralArray( getSpiralArray( 5 ) );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
const int n = 5;
|
||||
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
|
||||
int x = 0, y = -1, c = 0;
|
||||
vector<vector<int>> m(n, vector<int>(n));
|
||||
for (int i = 0, im = 0; i < n + n - 1; ++i, im = i % 4)
|
||||
for (int j = 0, jlen = (n + n - i) / 2; j < jlen; ++j)
|
||||
m[x += dx[im]][y += dy[im]] = ++c;
|
||||
for (auto & r : m) {
|
||||
for (auto & v : r)
|
||||
cout << v << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
const int n = 5;
|
||||
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
|
||||
int x = 0, y = -1, c = 0;
|
||||
vector<vector<int>> m(n, vector<int>(n));
|
||||
for (int i = 0, im = 0; i < n + n - 1; ++i, im = i % 4)
|
||||
for (int j = 0, jlen = (n + n - i) / 2; j < jlen; ++j)
|
||||
m[x += dx[im]][y += dy[im]] = ++c;
|
||||
for (auto & r : m) {
|
||||
for (auto & v : r)
|
||||
cout << v << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,17 +52,17 @@ namespace spiralmat
|
|||
{
|
||||
for (int j = 1; j <= n; j++)
|
||||
Console.Write("{0,3:D} ",
|
||||
ob.func(n, i, j)
|
||||
+ Convert.ToInt32(
|
||||
((j >= i) && (i == lev))
|
||||
? ((j - i) + 1)
|
||||
: ((j == ((n + 1) - lev) && (i > lev) && (i <= j)))
|
||||
? (n - 2 * (lev - 1) + (i - 1) - (n - j))
|
||||
: ((i == ((n + 1) - lev) && (j < i)))
|
||||
? ((n - 2 * (lev - 1)) + ((n - 2 * (lev - 1)) - 1) + (i - j))
|
||||
: ((j == lev) && (i > lev) && (i < ((n + 1) - lev)))
|
||||
? ((n - 2 * (lev - 1)) + ((n - 2 * (lev - 1)) - 1) + ((n - 2 * (lev - 1)) - 1) + (((n + 1) - lev) - i))
|
||||
: 0));
|
||||
ob.func(n, i, j)
|
||||
+ Convert.ToInt32(
|
||||
((j >= i) && (i == lev))
|
||||
? ((j - i) + 1)
|
||||
: ((j == ((n + 1) - lev) && (i > lev) && (i <= j)))
|
||||
? (n - 2 * (lev - 1) + (i - 1) - (n - j))
|
||||
: ((i == ((n + 1) - lev) && (j < i)))
|
||||
? ((n - 2 * (lev - 1)) + ((n - 2 * (lev - 1)) - 1) + (i - j))
|
||||
: ((j == lev) && (i > lev) && (i < ((n + 1) - lev)))
|
||||
? ((n - 2 * (lev - 1)) + ((n - 2 * (lev - 1)) - 1) + ((n - 2 * (lev - 1)) - 1) + (((n + 1) - lev) - i))
|
||||
: 0));
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.ReadKey();
|
||||
|
|
|
|||
|
|
@ -4,32 +4,32 @@
|
|||
#define valid(i, j) 0 <= i && i < m && 0 <= j && j < n && !s[i][j]
|
||||
int main(int c, char **v)
|
||||
{
|
||||
int i, j, m = 0, n = 0;
|
||||
int i, j, m = 0, n = 0;
|
||||
|
||||
/* default size: 5 */
|
||||
if (c >= 2) m = atoi(v[1]);
|
||||
if (c >= 3) n = atoi(v[2]);
|
||||
if (m <= 0) m = 5;
|
||||
if (n <= 0) n = m;
|
||||
/* default size: 5 */
|
||||
if (c >= 2) m = atoi(v[1]);
|
||||
if (c >= 3) n = atoi(v[2]);
|
||||
if (m <= 0) m = 5;
|
||||
if (n <= 0) n = m;
|
||||
|
||||
int **s = calloc(1, sizeof(int *) * m + sizeof(int) * m * n);
|
||||
s[0] = (int*)(s + m);
|
||||
for (i = 1; i < m; i++) s[i] = s[i - 1] + n;
|
||||
int **s = calloc(1, sizeof(int *) * m + sizeof(int) * m * n);
|
||||
s[0] = (int*)(s + m);
|
||||
for (i = 1; i < m; i++) s[i] = s[i - 1] + n;
|
||||
|
||||
int dx = 1, dy = 0, val = 0, t;
|
||||
for (i = j = 0; valid(i, j); i += dy, j += dx ) {
|
||||
for (; valid(i, j); j += dx, i += dy)
|
||||
s[i][j] = ++val;
|
||||
int dx = 1, dy = 0, val = 0, t;
|
||||
for (i = j = 0; valid(i, j); i += dy, j += dx ) {
|
||||
for (; valid(i, j); j += dx, i += dy)
|
||||
s[i][j] = ++val;
|
||||
|
||||
j -= dx; i -= dy;
|
||||
t = dy; dy = dx; dx = -t;
|
||||
}
|
||||
j -= dx; i -= dy;
|
||||
t = dy; dy = dx; dx = -t;
|
||||
}
|
||||
|
||||
for (t = 2; val /= 10; t++);
|
||||
for (t = 2; val /= 10; t++);
|
||||
|
||||
for(i = 0; i < m; i++)
|
||||
for(j = 0; j < n || !putchar('\n'); j++)
|
||||
printf("%*d", t, s[i][j]);
|
||||
for(i = 0; i < m; i++)
|
||||
for(j = 0; j < n || !putchar('\n'); j++)
|
||||
printf("%*d", t, s[i][j]);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,16 @@
|
|||
|
||||
int spiral(int w, int h, int x, int y)
|
||||
{
|
||||
return y ? w + spiral(h - 1, w, y - 1, w - x - 1) : x;
|
||||
return y ? w + spiral(h - 1, w, y - 1, w - x - 1) : x;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int w = atoi(argv[1]), h = atoi(argv[2]), i, j;
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++)
|
||||
printf("%4d", spiral(w, h, j, i));
|
||||
putchar('\n');
|
||||
}
|
||||
return 0;
|
||||
int w = atoi(argv[1]), h = atoi(argv[2]), i, j;
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++)
|
||||
printf("%4d", spiral(w, h, j, i));
|
||||
putchar('\n');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
(if (= 1 n) row
|
||||
;; first row, plus (n-1) x m spiral rotated 90 degrees
|
||||
(append row (map 'list #'reverse
|
||||
(apply #'mapcar #'list
|
||||
(spiral (1- n) m (+ start m))))))))
|
||||
(apply #'mapcar #'list
|
||||
(spiral (1- n) m (+ start m))))))))
|
||||
|
||||
;; test
|
||||
(loop for row in (spiral 4 3) do
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ var S: string;
|
|||
begin
|
||||
S:='';
|
||||
for Y:=0 to High(Mat[0]) do
|
||||
begin
|
||||
S:=S+'[';
|
||||
for X:=0 to High(Mat) do
|
||||
S:=S+Format('%4.0f',[Mat[X,Y]]);
|
||||
S:=S+']'+#$0D#$0A;
|
||||
end;
|
||||
begin
|
||||
S:=S+'[';
|
||||
for X:=0 to High(Mat) do
|
||||
S:=S+Format('%4.0f',[Mat[X,Y]]);
|
||||
S:=S+']'+#$0D#$0A;
|
||||
end;
|
||||
Memo.Lines.Add(S);
|
||||
end;
|
||||
|
||||
|
|
@ -23,35 +23,35 @@ procedure MakeSpiralMatrix(var Mat: TMatrix; SizeX,SizeY: integer);
|
|||
var Inx: integer;
|
||||
var R: TRect;
|
||||
|
||||
procedure DoRect(R: TRect; var Inx: integer);
|
||||
{Create on turn of the spiral base on the rectangle}
|
||||
var X,Y: integer;
|
||||
begin
|
||||
{Do top part of rectangle}
|
||||
for X:=R.Left to R.Right do
|
||||
begin
|
||||
Mat[X,R.Top]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do Right part of rectangle}
|
||||
for Y:=R.Top+1 to R.Bottom do
|
||||
begin
|
||||
Mat[R.Right,Y]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do bottom part of rectangle}
|
||||
for X:= R.Right-1 downto R.Left do
|
||||
begin
|
||||
Mat[X,R.Bottom]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do left part of rectangle}
|
||||
for Y:=R.Bottom-1 downto R.Top+1 do
|
||||
begin
|
||||
Mat[R.Left,Y]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
end;
|
||||
procedure DoRect(R: TRect; var Inx: integer);
|
||||
{Create on turn of the spiral base on the rectangle}
|
||||
var X,Y: integer;
|
||||
begin
|
||||
{Do top part of rectangle}
|
||||
for X:=R.Left to R.Right do
|
||||
begin
|
||||
Mat[X,R.Top]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do Right part of rectangle}
|
||||
for Y:=R.Top+1 to R.Bottom do
|
||||
begin
|
||||
Mat[R.Right,Y]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do bottom part of rectangle}
|
||||
for X:= R.Right-1 downto R.Left do
|
||||
begin
|
||||
Mat[X,R.Bottom]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
{Do left part of rectangle}
|
||||
for Y:=R.Bottom-1 downto R.Top+1 do
|
||||
begin
|
||||
Mat[R.Left,Y]:=Inx;
|
||||
Inc(Inx);
|
||||
end;
|
||||
end;
|
||||
|
||||
begin
|
||||
{Set matrix size}
|
||||
|
|
@ -61,10 +61,10 @@ R:=Rect(0,0,SizeX-1,SizeY-1);
|
|||
Inx:=0;
|
||||
{draw and deflate rectangle until spiral is done}
|
||||
while (R.Left<=R.Right) and (R.Top<=R.Bottom) do
|
||||
begin
|
||||
DoRect(R,Inx);
|
||||
InflateRect(R,-1,-1);
|
||||
end;
|
||||
begin
|
||||
DoRect(R,Inx);
|
||||
InflateRect(R,-1,-1);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
function spiral(integer dimension)
|
||||
integer side, curr, curr2
|
||||
sequence s
|
||||
s = repeat(repeat(0,dimension),dimension)
|
||||
side = dimension
|
||||
curr = 0
|
||||
for i = 0 to floor(dimension/2) do
|
||||
for j = 1 to side-1 do
|
||||
s[i+1][i+j] = curr -- top
|
||||
curr2 = curr + side-1
|
||||
s[i+j][i+side] = curr2 -- right
|
||||
curr2 += side-1
|
||||
s[i+side][i+side-j+1] = curr2 -- bottom
|
||||
curr2 += side-1
|
||||
s[i+side-j+1][i+1] = curr2 -- left
|
||||
curr += 1
|
||||
end for
|
||||
curr = curr2 + 1
|
||||
side -= 2
|
||||
end for
|
||||
|
||||
if remainder(dimension,2) then
|
||||
s[floor(dimension/2)+1][floor(dimension/2)+1] = curr
|
||||
end if
|
||||
|
||||
return s
|
||||
end function
|
||||
|
||||
? spiral(5)
|
||||
|
|
@ -35,7 +35,7 @@ PROGRAM SPIRAL
|
|||
x = x + 1
|
||||
array(x,y) = n
|
||||
n = n + 1
|
||||
END DO
|
||||
END DO
|
||||
IF (n > size*size-1) EXIT
|
||||
END DO
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import Text.Printf (printf)
|
|||
-- spiral is the first row plus a smaller spiral rotated 90 deg
|
||||
spiral 0 _ _ = [[]]
|
||||
spiral h w s = [s .. s+w-1] : rot90 (spiral w (h-1) (s+w))
|
||||
where rot90 = (map reverse).transpose
|
||||
where rot90 = (map reverse).transpose
|
||||
|
||||
-- this is sort of hideous, someone may want to fix it
|
||||
main = mapM_ (\row->mapM_ ((printf "%4d").toInteger) row >> putStrLn "") (spiral 10 9 1)
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
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
|
||||
|
|
@ -12,7 +12,7 @@ class Folder(){
|
|||
def spiral(n:Int) = {
|
||||
def dup(n:Int) = (1 to n).flatMap(i=>List(i,i)).toList
|
||||
val folds = n :: dup(n-1).reverse //define fold part lengths
|
||||
|
||||
|
||||
var array = new Array[Array[Int]](n,n)
|
||||
val fold = new Folder()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
function spiral_mat(n) {
|
||||
a = J(n*n, 1, 1)
|
||||
u = J(n, 1, -n)
|
||||
v = J(n, 1, 1)
|
||||
for (k=(i=n)-1; k>=1; i=i+2*k--) {
|
||||
j = 1..k
|
||||
a[j:+i] = u[j] = -u[j]
|
||||
a[j:+(i+k)] = v[j] = -v[j]
|
||||
}
|
||||
return(rowshape(invorder(runningsum(a)),n):-1)
|
||||
a = J(n*n, 1, 1)
|
||||
u = J(n, 1, -n)
|
||||
v = J(n, 1, 1)
|
||||
for (k=(i=n)-1; k>=1; i=i+2*k--) {
|
||||
j = 1..k
|
||||
a[j:+i] = u[j] = -u[j]
|
||||
a[j:+(i+k)] = v[j] = -v[j]
|
||||
}
|
||||
return(rowshape(invorder(runningsum(a)),n):-1)
|
||||
}
|
||||
|
||||
spiral_mat(5)
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
Function build_spiral(n)
|
||||
botcol = 0 : topcol = n - 1
|
||||
botrow = 0 : toprow = n - 1
|
||||
'declare a two dimensional array
|
||||
Dim matrix()
|
||||
ReDim matrix(topcol,toprow)
|
||||
dir = 0 : col = 0 : row = 0
|
||||
'populate the array
|
||||
For i = 0 To n*n-1
|
||||
matrix(col,row) = i
|
||||
Select Case dir
|
||||
Case 0
|
||||
If col < topcol Then
|
||||
col = col + 1
|
||||
Else
|
||||
dir = 1 : row = row + 1 : botrow = botrow + 1
|
||||
End If
|
||||
Case 1
|
||||
If row < toprow Then
|
||||
row = row + 1
|
||||
Else
|
||||
dir = 2 : col = col - 1 : topcol = topcol - 1
|
||||
End If
|
||||
Case 2
|
||||
If col > botcol Then
|
||||
col = col - 1
|
||||
Else
|
||||
dir = 3 : row = row - 1 : toprow = toprow - 1
|
||||
End If
|
||||
Case 3
|
||||
If row > botrow Then
|
||||
row = row - 1
|
||||
Else
|
||||
dir = 0 : col = col + 1 : botcol = botcol + 1
|
||||
End If
|
||||
End Select
|
||||
Next
|
||||
'print the array
|
||||
For y = 0 To n-1
|
||||
For x = 0 To n-1
|
||||
WScript.StdOut.Write matrix(x,y) & vbTab
|
||||
Next
|
||||
WScript.StdOut.WriteLine
|
||||
Next
|
||||
End Function
|
||||
|
||||
build_spiral(CInt(WScript.Arguments(0)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue