Advertisement
AvengersAssemble

Untitled

Feb 15th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Unit4.CollectionsLib;
  5.  
  6. namespace Lab_15_02
  7. {
  8.     class Program
  9.     {
  10.         static Random Num = new Random();
  11.         static BinTreeNode<int> BuildT(int x)
  12.         {
  13.             int a;
  14.             if (x < 0) return null;
  15.             if (x == 0) return new BinTreeNode<int>(Num.Next(1, 21));
  16.             if (x > 0)
  17.             {
  18.                 a = Num.Next(1, 4);
  19.                 if (a == 1)
  20.                     return new BinTreeNode<int>(null, Num.Next(1, 21), BuildT(x - 1));
  21.                 if (a == 2)
  22.                     return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), null);
  23.                 if (a == 3)
  24.                     return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), BuildT(x - 1));
  25.             }
  26.             return null;
  27.         }
  28.  
  29.         static int[] a = new int[5] { 14, 1, 73, 8, 94 };
  30.  
  31.  
  32.         //public static void sortbouble(int[] a)
  33.         //{
  34.         //    int i, j, temp;
  35.         //    for (i = 0; i < a.Length; i++)
  36.         //        for (j = 0; j < a.Length - i - 1; j++)
  37.         //        {
  38.         //            if (a[j] > a[j + 1])
  39.         //            {
  40.         //                temp = a[j + 1];
  41.         //                a[j + 1] = a[j];
  42.         //                a[j] = temp;
  43.         //            }
  44.         //        }
  45.         //    for (i = 0; i < a.Length; i++)
  46.         //        Console.Write(a[i] + ",");
  47.  
  48.         //}
  49.  
  50.         //public static int biginrang(int[] a, int start, int stop)
  51.         //{
  52.         //    int index = start;
  53.         //    int i, max = a[start];
  54.         //    for (i = start; i < stop; i++)
  55.         //    {
  56.         //        if (a[i] > max)
  57.         //        {
  58.         //            max = a[i];
  59.         //            index = i;
  60.         //        }
  61.         //    }
  62.         //    return index;
  63.         //}
  64.  
  65.         //public static void keeploop(int[] a)
  66.         //{
  67.         //    int i, temp1, temp2, p;
  68.         //    for (i = a.Length; i > 0; i--)
  69.         //    {
  70.         //        p = biginrang(a, 0, i);
  71.         //        temp1 = a[p];
  72.         //        temp2 = a[i - 1];
  73.         //        a[p] = temp2;
  74.         //        a[i - 1] = temp1;
  75.         //    }
  76.         //    for (i = 0; i < a.Length; i++)
  77.         //        Console.Write(a[i] + ",");
  78.  
  79.  
  80.         //}
  81.         public static BinTreeNode<int> AVL(int[] a, int i, int j)
  82.         {
  83.             if (i > j) return null;
  84.             int mid = (i + j) / 2;
  85.             int r = a[mid];
  86.             BinTreeNode<int> t1 = AVL(a, i, mid - 1);
  87.             BinTreeNode<int> t2 = AVL(a, mid + 1, j);
  88.             return new BinTreeNode<int>(t1, r, t2);
  89.         }
  90.  
  91.         public static Stack<ArithProg> ArithmeticStack(Stack<int> s)
  92.         {
  93.             Stack<ArithProg> s2 = new Stack<ArithProg>();
  94.             int a1, d, n, tmp;
  95.             while (!s.IsEmpty())
  96.             {
  97.                 n = 1;
  98.                 a1 = s.Pop();
  99.                 if (!s.IsEmpty())
  100.                 {
  101.                     tmp = s.Pop();
  102.                     d = tmp - a1;
  103.                     n++;
  104.                     if (!s.IsEmpty())
  105.                     {
  106.                         while (d == s.Top() - tmp)
  107.                         {
  108.                             n++;
  109.                             tmp = s.Pop();
  110.                         }
  111.                     }
  112.                     s.Push(tmp);
  113.                     s2.Push(new ArithProg(a1, d, n));
  114.                 }
  115.             }
  116.             s2.Pop();
  117.             s2.Top().N++;
  118.             return s2;
  119.         }
  120.         static void Main(string[] args)
  121.         {
  122.             int i = 0;
  123.             int j = 4;
  124.             //    sortbouble(a);
  125.             //keeploop(a);
  126.             //  Console .Write ( biginrang(a, 0, a.Length ));
  127.             //BinTreeNode<int> p = AVL(a, i, j);
  128.             //BinTreeUtils.ShowTree(p);
  129.             Stack<int> s = new Stack<int>();
  130.             s.Push(30);
  131.             s.Push(26);
  132.             s.Push(20);
  133.             s.Push(14);
  134.             s.Push(21);
  135.             s.Push(18);
  136.             s.Push(15);
  137.             s.Push(12);
  138.             s.Push(13);
  139.             s.Push(14);
  140.             s.Push(15);
  141.             s.Push(16);
  142.             s.Push(17);
  143.             Console.WriteLine(s.ToString());
  144.             Console.WriteLine(ArithmeticStack(s).ToString());
  145.         }
  146.  
  147.     }
  148.  
  149. }
  150.  
  151.  
  152. ///////////////
  153.  
  154. using System;
  155. using System.Collections.Generic;
  156. using System.Linq;
  157. using System.Text;
  158.  
  159. namespace Lab_15_02
  160. {
  161.     public class ArithProg
  162.     {
  163.         private int a1;
  164.         private int d;
  165.         private int n;
  166.         public ArithProg(int a1, int d, int n)
  167.         {
  168.             this.a1 = a1;
  169.             this.d = d;
  170.             this.n = n;
  171.         }
  172.         public int A1
  173.         {
  174.             get { return this.a1; }
  175.             set { this.a1 = value; }
  176.         }
  177.         public int D
  178.         {
  179.             get { return this.d; }
  180.             set { this.d = value; }
  181.         }
  182.         public int N
  183.         {
  184.             get { return this.n; }
  185.             set { this.n = value; }
  186.         }
  187.         public override string ToString()
  188.         {
  189.             return "first " + a1 + "\n" + "diff " + d + "\namount " + n + "\n\n";
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement