-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.FillTheMatrix
69 lines (64 loc) · 1.82 KB
/
01.FillTheMatrix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
class fillTheMatrix
{
static int size = 0;
static void Main()
{
size = int.Parse(Console.ReadLine());
MatrixA();
Console.WriteLine();
MatrixB();
}
static void MatrixA()
{
int[,] matrix = new int[size, size];
int counter = 1;
for (int cols = 0; cols < size; cols++)
{
for (int rows = 0; rows < size; rows++)
{
matrix[rows, cols] = counter;
counter++;
}
}
PrintMatrix(matrix);
}
static void MatrixB()
{
int[,] matrix = new int[size, size];
int counter = 1;
for (int cols = 0; cols < size; cols++)
{
if (cols % 2 == 0)
{
for (int rows = 0; rows < size; rows++)
{
matrix[rows, cols] = counter;
counter++;
}
}
else
{
for (int rows = size-1; rows >= 0; rows--)
{
matrix[rows, cols] = counter;
counter++;
}
}
}
PrintMatrix(matrix);
}
static void PrintMatrix(int[,] matrix)
{
for (int rows = 0; rows < matrix.GetLength(0); rows++)
{
for (int cols = 0; cols < matrix.GetLength(1); cols++)
{
Console.Write("{0,2} ", matrix[rows,cols]);
}
Console.WriteLine();
}
}
}