簡體   English   中英

在不使用Linq的情況下找到數組的Min-Max值?

[英]Find Min-Max values of array without using Linq?

盡管我已經解決了問題,但我一直在為我的編程類進行此作業。 對感興趣的人的一些信息:我的任務是從一系列分數中找到競爭對手的分數(例如,int []分數= 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

}
}

所以我以為我已經完成了,但是現在我已經收到並發電子郵件說:

“不能使用任何system.array方法,也不能使用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值加到總和上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM