Exemplo n.º 1
0
        public void DelMax_PQWith3Element_ReturnsGreater()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);
            pq.Insert(3);
            pq.Insert(2);

            Assert.AreEqual(3, pq.DelMax());
            Assert.AreEqual(2, pq.DelMax());
            Assert.AreEqual(1, pq.DelMax());
        }
        static void Main(string[] args)
        {
            // 输入格式: buy 20.05 100
            var buyer  = new MaxPQ <Ticket>();
            var seller = new MinPQ <Ticket>();

            var n = int.Parse(Console.ReadLine());

            for (var i = 0; i < n; i++)
            {
                var ticket = new Ticket();
                var item   = Console.ReadLine().Split(' ');

                ticket.Price = double.Parse(item[1]);
                ticket.Share = int.Parse(item[2]);
                if (item[0] == "buy")
                {
                    buyer.Insert(ticket);
                }
                else
                {
                    seller.Insert(ticket);
                }
            }

            while (!buyer.IsEmpty() && !seller.IsEmpty())
            {
                if (buyer.Max().Price < seller.Min().Price)
                {
                    break;
                }
                var buy  = buyer.DelMax();
                var sell = seller.DelMin();
                Console.Write("sell $" + sell.Price + " * " + sell.Share);
                if (buy.Share > sell.Share)
                {
                    Console.WriteLine(" -> " + sell.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                    buy.Share -= sell.Share;
                    buyer.Insert(buy);
                }
                else if (buy.Share < sell.Share)
                {
                    sell.Share -= buy.Share;
                    seller.Insert(sell);
                    Console.WriteLine(" -> " + buy.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                }
                else
                {
                    Console.WriteLine(" -> " + sell.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                }
            }
        }
Exemplo n.º 3
0
        public void IsEmpty_PQWithOneElement_ReturnsFalse()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);

            Assert.IsFalse(pq.IsEmpty());
        }
Exemplo n.º 4
0
        public void IsEmpty_AfterRemovingLastElement_ReturnsTrue()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);
            pq.DelMax();

            Assert.IsTrue(pq.IsEmpty());
        }
Exemplo n.º 5
0
        public void DelMax_PQWithOneElement_ReturnsThatElement()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);

            int retVal = pq.DelMax();

            Assert.AreEqual(1, retVal);
        }
Exemplo n.º 6
0
        static long Test(MaxPQ <int> pq, int n)
        {
            // 生成数据
            var initData   = new int[n];
            var appendData = new int[n / 2];

            for (var i = 0; i < n; i++)
            {
                initData[i] = random.Next();
            }
            for (var i = 0; i < n / 2; i++)
            {
                appendData[i] = random.Next();
            }

            // 开始测试
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            // 插入 n 个元素
            for (var i = 0; i < n; i++)
            {
                pq.Insert(initData[i]);
            }
            // 删去一半
            for (var i = 0; i < n / 2; i++)
            {
                pq.DelMax();
            }
            // 插入一半
            for (var i = 0; i < n / 2; i++)
            {
                pq.Insert(appendData[i]);
            }
            // 删除全部
            for (var i = 0; i < n; i++)
            {
                pq.DelMax();
            }

            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
        static void Main(string[] args)
        {
            //    P
            //    R P
            //    R P I
            //    R P O I
            //    P O I
            //    R P O I
            //    P O I
            //    O I
            //    O I I
            //    I I
            //    T I I
            //    I I
            //    Y I I
            //    I I
            //    I
            //
            //    Q
            //    U Q
            //    U Q E
            //    Q E
            //    E
            //
            //    U
            //
            //    E
            //

            MaxPQ <char> pq    = new MaxPQ <char>();
            string       input = "P R I O * R * * I * T * Y * * * Q U E * * * U * E";

            foreach (char c in input)
            {
                if (c == ' ')
                {
                    continue;
                }
                else if (c == '*')
                {
                    pq.DelMax();
                }
                else
                {
                    pq.Insert(c);
                }

                foreach (char n in pq)
                {
                    Console.Write(n + " ");
                }
                Console.WriteLine();
            }
        }
 public void Insert(T a)
 {
     if (m_maxPQ.Size() < m_max)
     {
         m_maxPQ.Insert(a);
     }
     else
     {
         if (m_comparer.Compare(m_maxPQ.Top(), a) > 0)
         {
             m_maxPQ.ChangeTop(a);
         }
     }
 }
Exemplo n.º 9
0
        public void TestMaxPQ() {
            var arr = CreateRandomArray(10);
            Insertion<int>.Sort(arr);
            StdOut.WriteLine(arr);

            var pq = new MaxPQ<int>();
            foreach (int i in arr) {
                pq.Insert(i);
            }

            while (!pq.IsEmpty) {
                StdOut.WriteLine("delete max: {0}", pq.DelMax());
            }
        }
Exemplo n.º 10
0
        public void TestMaxPQ()
        {
            int amounts = 10;

            MaxPQ pq = new MaxPQ(amounts);

            Random rand = new Random();
            for (int i = 0; i < amounts; i++)
                pq.Insert(rand.Next(0, 1000).ToString());

            Debug.WriteLine(Sort.Show(pq.PQs));
            pq.DelMax();
            Debug.WriteLine(Sort.Show(pq.PQs));
        }
Exemplo n.º 11
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyPQ.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyPQ.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In(string.Format("Files\\Sorting\\{0}", fieName));
            var words = @in.ReadAllStrings();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            var listStrings = words.ToList();


            var pq = new MaxPQ <string>(new StringComparer());

            //Fill Priority Queue
            foreach (var word in listStrings)
            {
                pq.Insert(word);
            }
            // print results
            foreach (var item in pq)
            {
                Console.WriteLine(item);
            }


            Console.ReadLine();
        }
        static long Test(MaxPQ <int> pq, int[] data)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < data.Length; i++)
            {
                pq.Insert(data[i]);
            }
            for (int i = 0; i < data.Length; i++)
            {
                pq.DelMax();
            }
            sw.Stop();
            return(sw.ElapsedMilliseconds);
        }
Exemplo n.º 13
0
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyPQ.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            var fieName = string.Empty;
            switch (fileNumber)
            {
                case "1":
                    fieName = "tinyPQ.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In(string.Format("Files\\Sorting\\{0}", fieName));
            var words = @in.ReadAllStrings();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            var listStrings = words.ToList();

            var pq = new MaxPQ<string>(new StringComparer());
            //Fill Priority Queue
            foreach (var word in listStrings)
            {
                pq.Insert(word);
            }
            // print results
            foreach (var item in pq)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
Exemplo n.º 14
0
        public void TestMaxPQ()
        {
            var arr = CreateRandomArray(10);

            Insertion <int> .Sort(arr);

            StdOut.WriteLine(arr);

            var pq = new MaxPQ <int>();

            foreach (int i in arr)
            {
                pq.Insert(i);
            }

            while (!pq.IsEmpty)
            {
                StdOut.WriteLine("delete max: {0}", pq.DelMax());
            }
        }
Exemplo n.º 15
0
        static int Test(int n, MaxPQ <int> pq)
        {
            var timer = new Timer(1000);

            timer.Elapsed += new ElapsedEventHandler(StopRunning);
            for (var i = 0; i < n; i++)
            {
                pq.Insert(random.Next());
            }

            var delCount = 0;

            StartRunning();
            timer.Start();
            while (isRunning && !pq.IsEmpty())
            {
                pq.DelMax();
                delCount++;
            }
            timer.Stop();
            return(delCount);
        }
Exemplo n.º 16
0
        public int LastStoneWeight(int[] stones)
        {
            MaxPQ maxPQ = new MaxPQ(stones);

            while (maxPQ.Count() >= 2)
            {
                int x = maxPQ.RemoveMax();
                int y = maxPQ.RemoveMax();
                if (x != y)
                {
                    maxPQ.Insert(Math.Abs(x - y));
                }
            }

            if (maxPQ.IsEmpty())
            {
                return(0);
            }
            else
            {
                return(maxPQ.RemoveMax());
            }
        }
        static void Main(string[] args)
        {
            var pq  = new MaxPQLinked <char>();
            var pq2 = new MaxPQ <char>();
            // 利用 2.4.6 的输入做测试
            var input = "P R I O * R * * I * T * Y * * * Q U E * * * U * E";

            foreach (var c in input)
            {
                if (c == ' ')
                {
                    continue;
                }
                else if (c == '*')
                {
                    Console.WriteLine(pq.DelMax() + " " + pq2.DelMax());
                }
                else
                {
                    pq.Insert(c);
                    pq2.Insert(c);
                }
            }
        }