• 求n个数中的最大或最小k个数


      //求n个数中的最小k个数
            public static void TestMin(int k, int n)
            {
                Random rd = new Random();
                int[] myArray = new int[n];
                int[] newArray = new int[k];

                for (int i = 0; i < n; i++)
                {
                    // randomNumber = rd.Next(10000, 10000000);
                    myArray[i] = rd.Next(1, 1000);
                    Console.WriteLine(myArray[i]);
                }
                Console.WriteLine("**********************");
                for (int i = 0; i < k; i++)
                {
                    newArray[i] = myArray[i];
                    Console.WriteLine(newArray[i]);
                }
                Console.WriteLine("**********************");
                Array.Sort(newArray);

                for (int j = k; j < n; j++)
                {
                    int max = newArray[k - 1];
                    //  Console.WriteLine("The last five" + newArray[i]);
                    if (max < myArray[j])
                    {
                        continue;
                    }
                    else
                    {
                        newArray[k - 1] = myArray[j];
                        Array.Sort(newArray);
                    }

                }

                foreach (int i in newArray)
                    Console.WriteLine(i);
            }
            //求n个数中的最大k个数
            public static void TestMax(int k, int n)
            {
                Random rd = new Random();
                int[] myArray = new int[n];
                int[] newArray = new int[k];

                for (int i = 0; i < n; i++)
                {
                    // randomNumber = rd.Next(10000, 10000000);
                    myArray[i] = rd.Next(1, 1000);
                    Console.WriteLine(myArray[i]);
                }
                Console.WriteLine("**********************");
                for (int i = 0; i < k; i++)
                {
                    newArray[i] = myArray[i];
                    Console.WriteLine(newArray[i]);
                }
                Console.WriteLine("**********************");
                Array.Sort(newArray);

                for (int j = k; j < n; j++)
                {
                    int max = newArray[0];
                    //  Console.WriteLine("The last five" + newArray[i]);
                    if (max > myArray[j])
                    {
                        continue;
                    }
                    else
                    {
                        newArray[0] = myArray[j];
                        Array.Sort(newArray);
                    }

                }

                foreach (int i in newArray)
                    Console.WriteLine(i);
            }

  • 相关阅读:
    PHP、asp、aspx、JSP一句话
    Linux 修改时区(PDT修改为CST)
    m0n0防火墙安装配置方法
    kali 使用John破解zip压缩包的密码
    Linux 挂载windows共享文件夹
    博客园添加粒子特效
    wpscan 更新超时报错
    kali 攻击wordpress + trunkey linux wordpress 安装方法
    Kali和Metasploitable2的网络配置
    设计模式之观察者
  • 原文地址:https://www.cnblogs.com/dasydong/p/3280963.html
Copyright © 2020-2023  润新知