Advertisement
marekov

PageCalculator

Jun 30th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. //75
  3. namespace PageCalculator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[] input = Console.ReadLine().
  10.                 Split(new[] { ' ', ',' },
  11.                 StringSplitOptions.RemoveEmptyEntries);
  12.             int wordsPerPage = int.Parse(Console.ReadLine());
  13.             string keyWord = Console.ReadLine();
  14.  
  15.             string[,] matrix = FillMatrix(input, wordsPerPage);
  16.             int page = IsPresent(matrix, keyWord);
  17.             Console.WriteLine(page);
  18.  
  19.         }
  20.         public static string[,] FillMatrix(string[] tokens, int words)
  21.         {
  22.             string[,] matrix = new string[tokens.Length / words, words];
  23.             int iterator = 0;
  24.  
  25.             for (int row = 0; row < matrix.GetLength(0); row++)
  26.             {
  27.                 for (int col = 0; col < matrix.GetLength(1); col++)
  28.                 {
  29.                     matrix[row, col] = tokens[iterator++];
  30.                 }
  31.             }
  32.             return matrix;
  33.         }
  34.         public static int IsPresent(string[,] arr, string word)
  35.         {
  36.             bool isPresent = false;
  37.             int index = 0;
  38.             for (int row = 0; row < arr.GetLength(0); row++)
  39.             {
  40.                 for (int col = 0; col < arr.GetLength(1); col++)
  41.                 {
  42.                     string w = arr[row, col];
  43.                     if (w == word)
  44.                     {
  45.                         isPresent = true;
  46.                         index = row;
  47.                     }
  48.                 }
  49.             }
  50.             if (isPresent)
  51.             {
  52.                 return index + 1;
  53.             }
  54.             else
  55.             {
  56.                 return -1;
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement