查找数组的最小 - 最大值,而无需使用LINQ? [英] Find Min-Max values of array without using Linq?

查看:124
本文介绍了查找数组的最小 - 最大值,而无需使用LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对这种分配为我的编程类一点点,虽然我有它解决。有兴趣的人士提供一些信息:我的任务是从分数的数组找到竞争对手的分数(例如,INT [] =得分4,5,6,7,8,9)。但增加分数的其余部分时(如,比分将= 5 + 6 + 7 + 8(不包括4和9)),我必须不包括最高和最低值。

I've been working on this assignment for my programming class for a little bit and though I had it solved. A little info for anyone interested: My task is to find the score of the competitor from an array of scores (eg, int[] scores = 4, 5, 6 , 7, 8, 9). BUT I must not include the highest and lowest values when adding the rest of the scores together (eg, score would = 5+6+7+8 (no including 4 and 9)).

我的解决方案时,我们被允许使用LINQ如下:

My solution when we were allowed to use linq was as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scoring {
class Program {


    static void Main(string[] args) {
        int[] scores = { 4, 7, 9, 3, 8, 6 };

        remove_duplicates(scores);
        console_text(scores);


        ExitProgram();
    }


    static void remove_duplicates(int[] scores) { //removes duplicat values from array

        var pScores = scores.Distinct().ToArray();

    }



    static int result(int[] pScores) { //calculates results by removing max and min scores, then adding the remaining.

        return pScores.Sum() - pScores.Max() - pScores.Min();

    }


    static void console_text(int[] pScores) { //renders the text in the consol window

        int final_score = result(pScores);


        Console.Write("Competitor your scores were " );
        foreach (int i in pScores) {
            Console.Write(" " + i);
        }
        Console.WriteLine("\r\n" + "           and your score was " + final_score);

    }





    static void ExitProgram() {
        Console.Write("\n\nPress any key to exit program: ");
        Console.ReadKey();
    }//end ExitProgram

}
}

所以我想我做的,但现在我已经收到和电子邮件指出:

So I thought I was done, but now I have recieved and email stating:

不能使用任何的System.Array方法也不能使用Linq的任何功能或任何其他库班你会发现

"cannot use any system.array methods nor can you use any functionality of Linq or any other library class you may discover"

这有我有点失落和迷茫,任何帮助都将是AP preciated。

This has me a bit lost and confused, any help at all would be appreciated.

感谢

推荐答案

下面是一个选项。这不是很清楚的唯一事情是天气,如果有两个这也是最小值或最大值,它们应该被删除两个或只有一个最高值和一个分相同的值。我删除只是一个分和一个最大值:

Here is one option. The only thing that is not very clear is weather if there are two equal values which are also min values or max values they should be removed both or only one max value and one min. I remove just one min and one max value:

int[] arr = new int[] { 2, 2, 3, 4, 5, 6, 6 };

        int Lowest = arr[0];
        int Highest = arr[0];
        int sum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (Lowest > arr[i])
            {
                Lowest = arr[i];
            }
            if (Highest < arr[i])
            {
                Highest = arr[i];
            }
            sum += arr[i];
        }
        sum -= (Lowest + Highest);
        Console.WriteLine("The sum withot the highest and lowest score is : {0}", sum);

如果你并删除所有重复的最小和最大分数的总和,最后得分前补充一点:

and if you and to remove all duplicated min and max scores add this before summing the final score:

int[] arrOfHighest = new int[arr.Length];
        int[] arrOfLowest = new int[arr.Length];
        int minCounter = 0;
        int maxCounter = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (Lowest == arr[i])
            {
                arrOfLowest[minCounter] = arr[i];
                minCounter++;
            }
            if (Highest == arr[i])
            {
                arrOfHighest[maxCounter] = arr[i];
                maxCounter++;
            }
        }
        int sumLoewst = 0;
        for (int i = 0; i < arrOfLowest.Length; i++)
        {
            sumLoewst += arrOfLowest[i];
        }
        int sumHighest = 0;
        for (int i = 0; i < arrOfHighest.Length; i++)
        {
            sumHighest += arrOfHighest[i];
        }
        sum -= (sumLoewst + sumHighest);

因此​​,基于附加信息的最后联系。既然你想要的最小和最大值进行一次添加的每个在矿井code以上 - 总和 - =(sumLoewst + sumHighest); 此插件之后:

sum -= (sumLoewst + sumHighest); 
sum += (Highest + Lowest);

这样,您将拥有其他所有值相加,不管他们怎样次出现在数组中为止。只有一个 MAX 并加入到总和有一个 MIN 值。

This way you will have all other values summed, no matter how times they appear in the array. And only one MAX and one MIN value added to the sum.

这篇关于查找数组的最小 - 最大值,而无需使用LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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