简体   繁体   English

在我的课堂上,我必须创建一种方法来查找数组C#中的多个最大数

[英]In my class i have to creating a method to find multiple maximum numbers in an array C#

In my class i have to create a method that will find multiple maximum numbers within an array. 在我的课堂上,我必须创建一个方法,该方法将在数组中找到多个最大数。 the user will input how many maximum numbers they are wanting and then the code should display all those numbers. 用户将输入他们想要的最大数量,然后代码应显示所有这些数字。 if i select 2 this code will show me the 2 maximum numbers but if im wanting more it just doesnt work. 如果我选择2,则此代码将向我显示2个最大数字,但如果即时通讯想要更多,它将无法正常工作。 i was trying to search the array for the maximum number and then have it display the outcome and then change that element to 0 and than repeat the loop until it hits that number (userinput). 我试图在数组中搜索最大数量,然后让它显示结果,然后将该元素更改为0,然后重复循环直到达到该数量(用户输入)。 im soo frustrated can someone help me please 我很沮丧有人可以帮我吗

    public static int FindingMaxNum(int[] arr, int max)
    { 
        int userinput;
        Console.Write("Enter the number of maximum values: ");
        userinput = Convert.ToInt32(Console.Read());

        int i = 0;
        int c = 0;
        for (i = 0; i < arr.Length; i++)
        { 
         if (arr[i] >= max)
         {
             max = arr[i];
             c++;

             while (c <= userinput)
             {
                Console.WriteLine(max);
                arr[i] = 0;
                break;
             }
          }

        }
        return -1;
     }

Simple solution with average complexity O(nlogn): Sort the array first then print the last N nos. 平均复杂度为O(nlogn)的简单解决方案:首先对数组进行排序,然后输出最后N个数字。

 int[] arr = new int[]{-5,-69,1250,24,-96,32578,11,124};
   Array.Sort(arr);
   int n=3;
   for(int i=arr.Length-1;i>=0 && n>0 ;--i){
       n--;
    Console.WriteLine(arr[i]);
   }

Now coming to your code, there are multiple problems. 现在来看您的代码,这里有多个问题。 First there is no need to take 'max' as parameter in your function. 首先,您无需在函数中使用“ max”作为参数。 Second you are looping only arr.Length times once. 其次,您仅循环一次arr.Length次。 There should be nested loop, outer one of which has to run userInput times and inner on has to iterate over all the values. 应该有嵌套循环,其中一个外部循环必须运行userInput时间,而内部循环必须遍历所有值。 Third you should initialize to extracted value position to minimum value so that the method works for negative numbers too. 第三,您应该将提取的值位置初始化为最小值,以便该方法也适用于负数。

Refined code: 完善的代码:

 for(int i=0;i<userinput;++i){
        int max = int.MinValue,pos=-1;
        for(int j=0;j<arr.Length;++j){
            if(max<arr[j]){
                pos = j;
                max = arr[j];
            }
        }
        arr[pos] = int.MinValue;
        Console.Write(max+",");
    }

If I understand correctly, you want to get some number of items from an array whose values are greater than all the others in the array. 如果我理解正确,则希望从数组中获取一些项目,这些项目的值大于数组中所有其他项的值。

If so, something like this modified version of your method may do the trick: 如果是这样,您可以使用这种方法的修改版本来解决问题:

public static void WriteMaxNum(int[] arr)
{
    if (arr == null)
    {
        Console.WriteLine("The array is null");
        return;
    }

    if (arr.Length == 0)
    {
        Console.WriteLine("The array is empty");
        return;
    }

    int count = arr.Length;
    int numMaxValues;

    // Get number of max values to return from user
    do
    {
        Console.Write("Enter the number of maximum values (1 - {0}): ", count);
    } while (!int.TryParse(Console.ReadLine(), out numMaxValues) ||
             numMaxValues < 1 ||
             numMaxValues > count);

    // Output the max values
    Console.Write("The {0} max values are: ", numMaxValues);
    Console.WriteLine(string.Join(", ", arr.OrderByDescending(i => i).Take(numMaxValues)));
}

Usage 用法

static void Main(string[] args)
{
    var myArray = new[] { 1, 9, 4, 8, 2, 5, 0, 7, 6, 3 };
    WriteMaxNum(myArray);

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Output 产量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM