在不使用Linq的情况下找到数组的最小最大值

本文关键字:数组 最大值 情况下 Linq | 更新日期: 2023-09-27 18:19:00

我一直在为我的编程课做这个作业,虽然我已经解决了。给感兴趣的人一点信息:我的任务是从分数数组中找到竞争对手的分数(例如,int[] scores = 4,5,6,7,8,9)。但是在将其余分数加在一起时,我不能包括最高和最低的值(例如,分数将= 5+6+7+8(不包括4和9))。

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

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
}
}

所以我以为我完成了,但现在我收到了一封电子邮件,上面写着:

"不能使用任何系统。你也不能使用Linq的任何功能或任何其他库类,你可能会发现"

这让我有点困惑,任何帮助都将是感激的。

谢谢

在不使用Linq的情况下找到数组的最小最大值

这里有一个选择。唯一不太清楚的是,如果有两个相等的值,也是最小值或最大值,他们应该同时删除或只删除一个最大值和一个最小值。我只删除一个最小值和一个最大值:

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);

,如果你想删除所有重复的最小和最大分数,在求和最终分数之前添加这个:

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);

所以最后的触摸基于额外的信息。由于您希望在上面的代码中只添加一次最小值和最大值- sum -= (sumLoewst + sumHighest);在此之后添加:

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

这样,无论它们在数组中出现多少次,都将对所有其他值求和。并且只有一个MAX和一个MIN值加到和中