K
K
K1ald2021-11-11 20:59:54
C++ / C#
K1ald, 2021-11-11 20:59:54

How to fill a matrix with natural numbers in the following order in C#?

618d59ff263b3795742576.jpeg

How to make this table and fill it as shown in the picture?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2021-11-11
@K1ald

static void Main(string[] args)
        {
            Console.WriteLine("Введите n:");
            int n = Int32.Parse(Console.ReadLine());


            //Строим матрицу
            int[,] mx = new int[n,n];
            bool invert = true;
            for (int i = 0, c = 1; i < n; i++) {
                for (int j = 0; j < n; j++,c++) {
                    int inx = invert ?  n - j - 1: j;
                    mx[inx, i] = c;
                }
                invert = !invert;
            }

            //Выводим
            int maxWidth = (n * n).ToString().Length;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                for (int  j = 0;  j < n; j++) 
                    sb.Append(mx[i, j]).Append(' ', maxWidth - mx[i, j].ToString().Length+1);
                sb.AppendLine();
            }
            Console.WriteLine(sb);

            Console.ReadKey();

        }

618d6cb9c368b225095390.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question