zoukankan      html  css  js  c++  java
  • 求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);
            }

  • 相关阅读:
    出乎意料的else语句
    2015年全国谷歌卫星地图离线数据存储方案
    Convert Sorted List to Binary Search Tree
    程序猿学习资料分享---爱分享的程序猿(新浪微博)
    myeclipse中断点调试
    J2EE之13个规范标准概念
    ora-01036: 非法的变量名/编号
    CCNA Cloud CLDFND 210-451 QUIZ: Server Virtualization
    已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
    UML类图中的几种关系总结
  • 原文地址:https://www.cnblogs.com/dasydong/p/3280963.html
Copyright © 2011-2022 走看看