simonradev

Bah Maamu I Muki

Nov 25th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5. namespace SortedIterativeWay
  6. {
  7.     class Program
  8.     {
  9.         static int[] array;
  10.         static int arrLen;
  11.         static int iterations;
  12.        
  13.         public static void Main(string[] args)
  14.         {
  15.             iterations = int.Parse(Console.ReadLine());
  16.             arrLen = int.Parse(Console.ReadLine());
  17.            
  18.             array = new int[arrLen];
  19.            
  20.             InitTheArr();
  21.            
  22.             NestedLoops();
  23.         }
  24.        
  25.         static void NestedLoops()
  26.         {
  27.             while (true)
  28.             {
  29.                 PrintRow();
  30.                
  31.                 int currentPosition = array.Length - 1;
  32.                 array[currentPosition]++;
  33.                
  34.                 while (array[currentPosition] > iterations)
  35.                 {
  36.                     if (currentPosition - 1 >= 0)
  37.                     {
  38.                         array[currentPosition] = array[currentPosition - 1];
  39.                     }
  40.                     currentPosition--;
  41.                    
  42.                     if (currentPosition < 0)
  43.                     {
  44.                         return;
  45.                     }
  46.                    
  47.                     array[currentPosition]++;
  48.                    
  49.                     if (currentPosition + 1 <= array.Length && array[currentPosition] > iterations)
  50.                     {
  51.                         array[currentPosition + 1] = iterations;
  52.                     }
  53.                     else if (currentPosition + 1 <= array.Length && array[currentPosition] <= iterations)
  54.                     {
  55.                         array[currentPosition + 1] = array[currentPosition];
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.        
  61.         static void PrintRow()
  62.         {
  63.             Console.WriteLine(string.Join(" ", array));
  64.         }
  65.        
  66.         static void InitTheArr()
  67.         {
  68.             for (int i = 0; i < array.Length; i++)
  69.             {
  70.                 array[i] = 1;
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment