Beispiel #1
0
        public void IsEmpty_PQWithOneElement_ReturnsFalse()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);

            Assert.IsFalse(pq.IsEmpty());
        }
Beispiel #2
0
        public void IsEmpty_AfterRemovingLastElement_ReturnsTrue()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

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

            Assert.IsTrue(pq.IsEmpty());
        }
        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");
                }
            }
        }
Beispiel #4
0
    public void maxpq()
    {
        MaxPQ <string> pq = new MaxPQ <string>();

        string item = "vonNeumann  2/12/1994  4732.35";

        if (!item.Equals("-"))
        {
            pq.insert(item);
        }
        else if (!pq.IsEmpty())
        {
            print(pq.delMax() + " ");
        }
        print("(" + pq.size() + " left on pq)");
    }
Beispiel #5
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);
        }
Beispiel #6
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());
            }
        }
Beispiel #7
0
        public void IsEmpty_InitalizedPQ_ReturnsTrue()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            Assert.IsTrue(pq.IsEmpty());
        }