2.4.37

上次更新:2019-04-17
发现了题解错误/代码缺陷/排版问题?请点这里:如何:提交反馈

解答

建立一个全局变量 isRunning ,每次 DelMax() 之前都先确认这个值是否为 true
设立一个 Timer 在 1 秒钟之后自动将 isRunning 置为 false

测试结果如下:

随着 n 增大,一秒钟之内能执行的 DelMax() 次数会下降。

代码

using System;
using System.Timers;
using PriorityQueue;

namespace _2._4._37
{
    class Program
    {
        static bool isRunning = true;
        static Random random = new Random();

        static void Main(string[] args)
        {
            int doubleTime = 6;
            int repeatTime = 6;
            int n = 1000000;
            for (int i = 0; i < doubleTime; i++)
            {
                int totalDelCount = 0;
                Console.WriteLine("n=" + n);
                for (int j = 0; j < repeatTime; j++)
                {
                    MaxPQ<int> pq = new MaxPQ<int>(n);
                    int delCount = Test(n, pq);
                    totalDelCount += delCount;
                    Console.Write(delCount + "\t");
                }
                Console.WriteLine("平均最大删除次数:" + totalDelCount / repeatTime);
                n *= 2;
            }
        }

        static int Test(int n, MaxPQ<int> pq)
        {
            Timer timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(StopRunning);
            for (int i = 0; i < n; i++)
            {
                pq.Insert(random.Next());
            }

            int delCount = 0;
            StartRunning();
            timer.Start();
            while (isRunning && !pq.IsEmpty())
            {
                pq.DelMax();
                delCount++;
            }
            timer.Stop();
            return delCount;
        }

        static void StartRunning() => isRunning = true;
        static void StopRunning(object source, ElapsedEventArgs e)
            => isRunning = false;
    }
}

另请参阅

PriorityQueue 库

上一题 下一题