Advertisement
desislava_topuzakova

5.

May 19th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Snake
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string input = Console.ReadLine();       //"3 4".Split() -> ["3", "4"]
  10.             int rows = int.Parse(input.Split()[0]);  //бр. редове
  11.             int cols = int.Parse(input.Split()[1]);  //бр. колони
  12.  
  13.             char[,] matrix = new char[rows, cols];
  14.             string snake = Console.ReadLine();
  15.  
  16.             int index = 0; //обхождаме snake
  17.            
  18.  
  19.             for (int row = 0; row < rows; row++)
  20.             {
  21.                 if (row % 2 == 0)
  22.                 {
  23.                     //четен ред -> от първа колона към последна
  24.                     for (int col = 0; col < cols; col++)
  25.                     {
  26.                         matrix[row, col] = snake[index];
  27.                         index++;
  28.                         if (index >= snake.Length)
  29.                         {
  30.                             index = 0;
  31.                         }
  32.                     }
  33.                 }
  34.                 else
  35.                 {
  36.                     //нечетен ред -> от последна колона към първа
  37.                     for (int col = cols - 1; col >= 0; col--)
  38.                     {
  39.                         matrix[row, col] = snake[index];
  40.                         index++;
  41.                         if (index >= snake.Length)
  42.                         {
  43.                             index = 0;
  44.                         }
  45.  
  46.                     }
  47.  
  48.                 }
  49.             }
  50.  
  51.             PrintMatrix(matrix);
  52.  
  53.  
  54.         }
  55.         private static void PrintMatrix(char[,] matrix)
  56.         {
  57.             for (int row = 0; row < matrix.GetLength(0); row++)
  58.             {
  59.                 for (int col = 0; col < matrix.GetLength(1); col++)
  60.                 {
  61.                     Console.Write(matrix[row, col]);
  62.                 }
  63.                 Console.WriteLine();
  64.             }
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement