在一次包含一组n个项目的项目列表中选择最大值和最小值 [英] To select max and min value in a list of items taking a group of n items at a time

查看:112
本文介绍了在一次包含一组n个项目的项目列表中选择最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在一次列出一组n个项目的项目列表中选择最大值和最小值,以便对每n个项目将它们添加到组中的顺序中的每n个项目具有最大值最小值对. br/> 例如,如果我们总共有10个项目作为
{1,2,3,4,5,6,7,8,9,10}
我们需要一次取3并获得max,min值,我们应该得到
{{1,3},{4,6},{7,9},{10,10})这些最小值,最大值.

To select max and min value in a list of items taking a group of n items at a time, so that we have max value min value pairs for every n number of items in the sequence they are added to the group.
For eg if we have total 10 items as
{ 1,2,3,4,5,6,7,8,9,10 }
and we need to take 3 at a time, and get the max , min values, we should get
{ {1,3}, {4,6},{7,9}, {10,10} ) these min,max values.

推荐答案

让m是初始项目列表的长度.使用双循环扫描列表:(i = 0; i< m; i + = n)的外循环.

在此外循环中,使用i处的值初始化max和min,然后使用内循环(j = i + 1; j< Min(i + n,m); j ++)与j处的值进行比较. />
然后,您可以将最大/最小对添加到对的列表中.
Let m be the length of the initial list of items. Scan the list using a double loop: the outer loop with (i= 0; i < m; i+= n).

In this outer loop you initialize max and min with the value at i, then compare to the values at j using an inner loop (j= i + 1; j < Min(i + n, m); j++).

Then you can append the max/min pair to the list of pairs.




到目前为止,您做了什么?自己尝试一下,并遇到一些问题.我将在此处发布解决方案,但我还会给您另一个问题,并希望由您解决.

您问题的解决方案:
Hi,

What you have done so far ? try something yourself and come with some issue/problem. I am posting here the solution but i will also give you another question and expecting to resolve by you.

Solution for your question :
class calculate
    {
        int startIndex = 0;
        int count = 3;
        List<int[]> myResult = new List<int[]>();

        public new List<int[]> MaxMinFinder(int[] abc)
        {
            int[] value1 = new int[2];
            value1[0] = abc.Skip(startIndex).Take(count).Max();
            value1[1] = abc.Skip(startIndex).Take(count).Min();
            myResult.Add(value1);
            startIndex += count;
            if (startIndex <= abc.Length)
            {
                MaxMinFinder(abc);
            }

            return myResult;
        }
    }



您可以使用此函数来获取List< int []>(您的预期结果).

现在你的功课是什么,
-使以上类概化,我不希望有任何固定值.在上面的代码中,您可以看到 count = 3 是正确的.使它普遍适用于任何地方.

希望您可以做到,即使这个问题比您的要简单得多,

祝你好运
谢谢
-Amit Gajjar



You can use this function to get List<int[]>(your expected result).

Now whats your homework is,
- Make above class generalize, i do not want any fix value. in above code you can see count = 3 is fix. make it generalize to use anywhere.

Hope you can do this, even this problem is very simpler then yours,

Best luck
Thanks
-Amit Gajjar


这篇关于在一次包含一组n个项目的项目列表中选择最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆