Advertisement
n4wn4w

C# list STACK OPASHKA

May 2nd, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. //  list<T>  ////////////
  2.  
  3.  
  4. int[] arr1 = {1,2,3,4,5,6,7};
  5.             int[] arr2 = {2,3,4,5,6,7,8};
  6.  
  7.             int[] kur = new int[0];
  8.             kur = Union(arr1, arr2);
  9.  
  10.             for (int i = 0; i < 8; i++)
  11.             {
  12.                 Console.WriteLine(kur[i] + " ");
  13.             }
  14.         }
  15.          public static int[] Union(int[] firstarr, int[] secondarr)
  16.         {
  17.             List<int> union = new List<int>();
  18.  
  19.             union.AddRange(firstarr);
  20.  
  21.             foreach (var item in secondarr)
  22.             {
  23.                  if (! union.Contains(item))
  24.               {
  25.                   union.Add(item);
  26.               }
  27.             }
  28.             return union.ToArray();
  29.         }
  30.  
  31.  
  32.  
  33. ///////// 2 primer vzima samo suvpadashtite 4isla ot 2ta masiva ///////////////////
  34.  
  35.  
  36.   static void Main(string[] args)
  37.         {
  38.  
  39.             int[] arr1 = {2,3,4,5,6,9};
  40.             int[] arr2 = {7,8,2,5};
  41.  
  42.             int[] kur = new int[0];
  43.             kur = Inter(arr1, arr2);
  44.  
  45.             for (int i = 0; i < 22; i++)
  46.             {
  47.                 Console.WriteLine(kur[i] + " ");
  48.             }
  49.            
  50.         }
  51.          public static int[] Inter(int[] firstarr, int[] secondarr)
  52.         {
  53.             List<int> union = new List<int>();
  54.  
  55.             foreach (var item in firstarr)
  56.             {
  57.                  if (Array.IndexOf(secondarr, item) != -1)
  58.               {
  59.                   union.Add(item);
  60.               }
  61.             }
  62.  
  63.             return union.ToArray();
  64.         }
  65.  
  66.  
  67.  
  68. ////////////// stack  primer sus stek  ////////////////////////////////////////////
  69.  
  70.  
  71.  Stack<string> stek = new Stack<string>();
  72.  
  73.             stek.Push("1. ivan");
  74.             stek.Push("2. divan");
  75.             stek.Push("3. many");
  76.             stek.Push("4. floyd");
  77.             stek.Push("5. kondio");
  78.  
  79.             Console.WriteLine("top = {0}", stek.Peek());
  80.  
  81.             while (stek.Count > 0)
  82.             {
  83.                 string name = stek.Pop();
  84.                 Console.WriteLine(name);
  85.             }
  86.  
  87.  
  88. //////////////////// stack primer brackets  /////////////////////////////////
  89.  
  90.  
  91.  string expr = "1 + (2 - (2+3) * 4 / (3+1)) + 5";
  92.  
  93.             Stack<int> stek = new Stack<int>();
  94.  
  95.             for (int i = 0; i < expr.Length; i++)
  96.             {
  97.                 char ch = expr[i];
  98.                 if (ch == '(')
  99.                 {
  100.                     stek.Push(i);
  101.                 }
  102.                 else if (ch == ')')
  103.                 {
  104.                     int startIndex = stek.Pop();
  105.                     int length = i - startIndex + 1;
  106.                     string contents = expr.Substring(startIndex, length);
  107.                     Console.WriteLine(contents);
  108.                 }
  109.              }
  110.  
  111.  
  112. ////////////////////////////////// stack  primer s start i end index  ///////////////////////////////////////
  113.  
  114.  
  115.             int[] array = { 2, 3, -6, -1, 2, -1, 6, 4, -8, 8 };// ako si sloja 9 she mi zeme do 9
  116.             Stack<int> stek = new Stack<int>();
  117.             int currentSum = array[0];
  118.             int startIndex = 0;
  119.             int endIndex = 0;
  120.            
  121.             int maxSum = array[0];
  122.             int count = 0;
  123.  
  124.             for (int i = 1; i < array.Length; i++)
  125.             {
  126.                 if (currentSum < 0)
  127.                 {
  128.                     currentSum = array[i];
  129.                     stek.Push(i);
  130.                 }
  131.                 else
  132.                 {
  133.                     currentSum += array[i];
  134.                     count++;
  135.                 }
  136.  
  137.                 if (i != 1)
  138.                 {
  139.                      if (currentSum > maxSum)
  140.                 {
  141.                     maxSum = currentSum;
  142.                    
  143.                         startIndex = stek.Pop() + 1;
  144.  
  145.                         endIndex =  (i+1) - count +  startIndex;
  146.                 }
  147.                 }
  148.                
  149.             }
  150.  
  151.             Console.WriteLine("The best sum is: {0} ", maxSum);
  152.  
  153.             Console.WriteLine("The best sequence is:");
  154.  
  155.             for (int i = startIndex; i <= endIndex; i++)
  156.             {
  157.                 Console.Write(i != endIndex ? array[i] + ", " : array[i] + "\n");
  158.             }
  159.             Console.WriteLine();
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. ///////////////////////  queue  opashka primer ///////////////////////////////////////////////
  167.  
  168.  Queue<string> opshka = new Queue<string>();
  169.  
  170.             opshka.Enqueue("1 kondio");
  171.             opshka.Enqueue("2 ondio");
  172.             opshka.Enqueue("3 ndio");
  173.             opshka.Enqueue("4 dio");
  174.             opshka.Enqueue("5 io");
  175.  
  176.             while (opshka.Count > 0)
  177.             {
  178.                 string mas = opshka.Dequeue();
  179.                 Console.WriteLine(mas);
  180.             }
  181.  
  182.  
  183. /////////////////////  opshka  primer s 4isla  ///////////////////
  184.  
  185.             int n = 3;
  186.             int p = 16;
  187.  
  188.             Queue<int> opshka = new Queue<int>();
  189.  
  190.             opshka.Enqueue(n);
  191.  
  192.             int index = 0;
  193.             while (opshka.Count > 0)
  194.             {
  195.                 int current = opshka.Dequeue();
  196.                 index++;
  197.                 if (current == p)
  198.                 {
  199.                     Console.WriteLine("Index = {0}", index);
  200.                 }
  201.  
  202.                 opshka.Enqueue(current + 1);
  203.                 opshka.Enqueue(2 * current);
  204.             }
  205.  
  206.  
  207.  
  208. ////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement