Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Zig-zag-matrix/D/zig-zag-matrix-1.d
Normal file
24
Task/Zig-zag-matrix/D/zig-zag-matrix-1.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
int[][] zigZag(in int n) pure nothrow @safe {
|
||||
static void move(in int n, ref int i, ref int j)
|
||||
pure nothrow @safe @nogc {
|
||||
if (j < n - 1) {
|
||||
if (i > 0) i--;
|
||||
j++;
|
||||
} else
|
||||
i++;
|
||||
}
|
||||
|
||||
auto a = new int[][](n, n);
|
||||
int x, y;
|
||||
foreach (v; 0 .. n ^^ 2) {
|
||||
a[y][x] = v;
|
||||
(x + y) % 2 ? move(n, x, y) : move(n, y, x);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
writefln("%(%(%2d %)\n%)", 5.zigZag);
|
||||
}
|
||||
18
Task/Zig-zag-matrix/D/zig-zag-matrix-2.d
Normal file
18
Task/Zig-zag-matrix/D/zig-zag-matrix-2.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
int[][] zigZag(in int n) pure nothrow {
|
||||
static struct P2 { int x, y; }
|
||||
const L = iota(n ^^ 2).map!(i => P2(i % n, i / n)).array
|
||||
.sort!q{ (a.x + a.y == b.x + b.y) ?
|
||||
((a.x + a.y) % 2 ? a.y < b.y : a.x < b.x) :
|
||||
(a.x + a.y) < (b.x + b.y) }.release;
|
||||
|
||||
auto result = new typeof(return)(n, n);
|
||||
foreach (immutable i, immutable p; L)
|
||||
result[p.y][p.x] = i;
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writefln("%(%(%2d %)\n%)", 5.zigZag);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue