Advertisement
dimipan80

Pyramid

May 16th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. /* You are a given pyramid of integer numbers. Your task is to print a growing sequence of integers, starting from the top of the pyramid.
  2.  * If a row does not contain a number larger than the previous one, we go to the next row and search for a number greater than the previous number + 1.
  3.  * On the first line, you will get the number of lines N. On the next N you will get the rows of the pyramid. The numbers in each row are separated by one or more spaces. There will be a different number of spaces at the beginning of each line. */
  4.  
  5. namespace Pyramid
  6. {
  7.     using System;
  8.     using System.Collections.Generic;
  9.     using System.Linq;
  10.  
  11.     class Pyramid
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int count = int.Parse(Console.ReadLine());
  16.             List<int> growingList = new List<int>();
  17.             int maxNum = int.MinValue;
  18.             for (int i = 0; i < count; i++)
  19.             {
  20.                 int[] nums =
  21.                     Console.ReadLine()
  22.                         .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  23.                         .Select(int.Parse)
  24.                         .ToArray();
  25.  
  26.                 Array.Sort(nums);
  27.                 bool isFoundLarger = false;
  28.                 for (int j = 0; j < nums.Length; j++)
  29.                 {
  30.                     if (nums[j] > maxNum)
  31.                     {
  32.                         maxNum = nums[j];
  33.                         isFoundLarger = true;
  34.                         growingList.Add(maxNum);
  35.                         break;
  36.                     }
  37.                 }
  38.  
  39.                 if (!isFoundLarger)
  40.                 {
  41.                     maxNum++;
  42.                 }
  43.             }
  44.  
  45.             Console.WriteLine(string.Join(", ", growingList));
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement