all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
22
Task/Zig-zag-matrix/D/zig-zag-matrix.d
Normal file
22
Task/Zig-zag-matrix/D/zig-zag-matrix.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
int[][] zigZag(in int n) pure nothrow {
|
||||
static void move(in int n, ref int i, ref int j) pure nothrow {
|
||||
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%)", zigZag(5));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue