Advertisement
CodeConf

Practice "Telerik Academy Exam 1 @ 7 Dec 2011

Jun 10th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. Problem 5 – Lines
  2. You are given a list of 8 bytes (positive integers in the range [0255]) 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:
  3.   7  6  5 4  3  2  1  0
  4. 0        ■                n0 = 8
  5. 1    ■         ■                n1 = 72
  6. 2        ■                n2 = 8
  7. 3        ■                n3 = 8
  8. 4    ■                n4 = 16
  9. 5    ■  ■  ■          n5 = 28
  10. 6 ■ ■ ■  ■              n6 = 240
  11. 7                   n7 = 0
  12.  
  13. 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.
  14. Input
  15. The input data is should be read from the console. There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7. It is guaranteed that there exists at least one line in the grid (the grid is not empty).
  16. The input data will always be valid and in the format described. There is no need to check it explicitly.
  17. Output
  18. The output consists of two integers placed on separate lines. The first line should hold the length of the longest line in the grid. The second line should hold the number of lines with the maximal length.
  19. Constraints
  20. • The numbers n0, n1, …, n7 are positive integers in the range [0255].
  21. • Allowed work time for your program: 0.25 seconds.
  22. • Allowed memory: 16 MB.
  23.  
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Text;
  29. using System.Threading.Tasks;
  30.  
  31.  
  32. class Lines
  33. {
  34.     static void Main()
  35.     {
  36.         int n0 = int.Parse(Console.ReadLine());
  37.         int n1 = int.Parse(Console.ReadLine());
  38.         int n2 = int.Parse(Console.ReadLine());
  39.         int n3 = int.Parse(Console.ReadLine());
  40.         int n4 = int.Parse(Console.ReadLine());
  41.         int n5 = int.Parse(Console.ReadLine());
  42.         int n6 = int.Parse(Console.ReadLine());
  43.         int n7 = int.Parse(Console.ReadLine());
  44.         int[] input = new int[] { n0, n1, n2, n3, n4, n5, n6, n7 };
  45.         char [] m=new char[8];
  46.         int[,] matrix=new int [8,8];
  47.  
  48.        
  49.         for (int row = 0; row < 8; row++)
  50.         {
  51.             string strN = Convert.ToString(input[row], 2).PadLeft(8, '0');
  52.             int bits = 7;
  53.             foreach (var item in strN)
  54.             {
  55.                 m[bits] = item;
  56.                 bits--;
  57.             }
  58.             for (int col = 7; col >= 0; col--)
  59.             {
  60.                 matrix[row,col]=(int)(m[col]-'0');
  61.             }
  62.         }
  63.         int maxLength = 0;
  64.         int currentLength = 0;
  65.         int maxCount = 0;
  66.  
  67.         for (int row = 0; row < 8; row++)
  68.         {
  69.             for (int col = 7; col >= 0; col--)
  70.             {
  71.                 while (matrix[row,col]==1)
  72.                 {
  73.                     currentLength++;
  74.                     if (currentLength > maxLength)
  75.                     {
  76.                         maxLength = currentLength;
  77.                         maxCount = 1;
  78.                     }
  79.                     else if (currentLength == maxLength)
  80.                     {
  81.                         maxCount++;
  82.                     }
  83.                     if (col == 0)
  84.                     {
  85.                         break;
  86.                     }
  87.                     col--;
  88.                 }
  89.                 currentLength = 0;  
  90.             }
  91.             currentLength = 0;
  92.         }
  93.         int maxColLength=0;
  94.         int maxColCount = 0;
  95.        
  96.         for (int col = 7; col >=0; col--)
  97.         {
  98.             for (int row = 0; row < 8; row++)
  99.             {
  100.                 while (matrix[row,col]==1)
  101.                 {
  102.                     currentLength++;
  103.                    
  104.                     if (currentLength > maxColLength)
  105.                     {
  106.                         maxColLength = currentLength;
  107.                         maxColCount = 1;
  108.                     }
  109.                     else if (currentLength == maxColLength)
  110.                     {
  111.                         maxColCount++;
  112.                     }
  113.                     if (row == 7)
  114.                     {
  115.                         break;
  116.                     }
  117.                     row++;
  118.                 }
  119.                 currentLength = 0;
  120.             }
  121.            
  122.             currentLength = 0;
  123.         }
  124.         int result;
  125.         if (maxLength==1 && maxColLength==1)
  126.         {
  127.             Console.WriteLine(maxLength);
  128.             Console.WriteLine(maxCount);
  129.         }
  130.         else if (maxLength==maxColLength)
  131.         {
  132.             result=maxCount + maxColCount;
  133.             Console.WriteLine(maxLength);
  134.             Console.WriteLine(result);
  135.         }
  136.         else if (maxLength>maxColLength)
  137.         {
  138.             Console.WriteLine(maxLength);
  139.             Console.WriteLine(maxCount);
  140.         }
  141.         else if(maxLength<maxColLength)
  142.         {
  143.             Console.WriteLine(maxColLength);
  144.             Console.WriteLine(maxColCount);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement