Advertisement
sylviapsh

Lines

Dec 27th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. class Lines
  4. {
  5.   static void Main()
  6.   {
  7.     //Telerik Academy
  8.     //You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns. Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last line is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered from the first (top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7. The figure shows a sample square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
  9.     //7 6   5   4   3   2   1   0  
  10. //0                 ■             n0 = 8
  11. //1     ■         ■             n1 = 72
  12. //2                 ■             n2 = 8
  13. //3                 ■             n3 = 8
  14. //4             ■                 n4 = 16
  15. //5             ■ ■ ■         n5 = 28
  16. //6 ■ ■ ■ ■                 n6 = 240
  17. //7                                 n7 = 0
  18. //A line is any sequence of full cells staying on the same row or column. At the figure above we have two lines of 4 cells and two lines of 3 cells and one line of 1 cell. You need to create a program that finds the longest line in the grid and the number of lines with the longest length. At the figure we have two largest lines with length of 4 cells.
  19.  
  20.     byte sum = 0,
  21.          counter = 0,
  22.          currentBiggestLine = 0,
  23.          currentBiggestCounter = 0,
  24.          biggestLine = 0,
  25.          biggestCounter = 0;
  26.     string [,] numsArray = new string [8,8];
  27.     string numToCheck="",
  28.            numElement = "";
  29.  
  30.     //Read the number from the console and input them in an array as strings.
  31.     for (int row = 0; row <= 7; row++)
  32.     {
  33.       byte input = byte.Parse(Console.ReadLine()); //Read the number.
  34.       string number = Convert.ToString(input, 2).PadLeft(8,'0'); //Convert the number to a binary represented string.
  35.       for (int column = 0; column <= 7; column++) //Populate the array with each character from the string.
  36.       {
  37.          numElement = StringInfo.GetNextTextElement(number, column);
  38.          numsArray[row, column] = numElement;
  39.       }
  40.       numElement = "";
  41.     }
  42.  
  43.     //Check the rows
  44.     for (int row = 0; row <= 7; row++)
  45.     {
  46.       numToCheck = "";
  47.       for (int column = 0; column <= 7; column++)
  48.       {
  49.  
  50.         string currentElement = numsArray[row, column];
  51.         numToCheck = numToCheck + currentElement;
  52.       }
  53.  
  54.       sum = 0;
  55.       counter = 0;
  56.       foreach (char item in numToCheck)
  57.       {
  58.         if (item == '1')
  59.         {
  60.           sum++;
  61.           if (sum == biggestLine)
  62.           {
  63.             counter++;
  64.             biggestCounter += counter;
  65.           }
  66.           else if (sum > biggestLine)
  67.           {
  68.             biggestLine = sum;
  69.             counter = 1;
  70.             biggestCounter = counter;
  71.           }
  72.         }
  73.  
  74.         if (item == '0')
  75.         {
  76.           sum = 0;
  77.           counter = 0;
  78.         }
  79.       }
  80.     }
  81.     currentBiggestCounter = biggestCounter;
  82.     currentBiggestLine = biggestLine;
  83.  
  84.     //Check the columns
  85.     sum = 0;
  86.     counter = 0;
  87.     for (int column = 0; column <= 7; column++)
  88.     {
  89.       numToCheck = "";
  90.         for (int row = 0; row <= 7; row++)
  91.       {
  92.  
  93.         string currentElement = numsArray[row, column];
  94.         numToCheck = numToCheck + currentElement;
  95.       }
  96.  
  97.  
  98.         sum = 0;
  99.         counter = 0;
  100.         foreach (char item in numToCheck)
  101.         {
  102.           if (item == '1')
  103.           {
  104.             sum++;
  105.             if (sum == currentBiggestLine)
  106.             {
  107.               counter++;
  108.               currentBiggestCounter += counter;
  109.             }
  110.             else if (sum > currentBiggestLine)
  111.             {
  112.               currentBiggestLine = sum;
  113.               counter = 1;
  114.               currentBiggestCounter = counter;
  115.             }
  116.           }
  117.  
  118.           if (item == '0')
  119.           {
  120.             sum = 0;
  121.             counter = 0;
  122.           }
  123.       }
  124.     }
  125.  
  126.     if (currentBiggestLine == biggestLine && biggestLine == 1)
  127.     {
  128.       Console.WriteLine(currentBiggestLine);
  129.       Console.WriteLine(currentBiggestCounter/2);
  130.     }
  131.     else
  132.     {
  133.       Console.WriteLine(currentBiggestLine);
  134.       Console.WriteLine(currentBiggestCounter);
  135.     }
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement