所以我有一個我需要做的測驗評估,這是我一直遇到的一個問題。我需要選擇最重的物體(最大的數字)并將它們按堆疊順序放置(底部重,頂部輕)我遇到的問題是我不知道從哪里開始,我的代碼我使用的是 C#,但是我知道我必須使用排序方法。所以我的主要問題是我試圖找出我開始的舉動以及如何從他們那里取得進展。另外我相信這可能是在 codin 游戲中,但還沒有在任何地方找到它,如果有人能找到它的名稱,那也會有很大幫助。

uj5u.com熱心網友回復:
我認為在這種情況下,呼叫Sort是一種矯枉過正,因為它用于IEnumerables,您沒有并且需要創建它。
我認為使用此頁面學習會更容易:https : //www.tutorialspoint.com/Chash-program-to-find-the-maximum-of-three-numbers
在這種情況下,排序似乎無濟于事,因為您需要回傳權重的索引,而不是大小。
您可以嘗試創建一個帶有索引和權重的字典,并使用 linq 找到最大的權重:
public static int GetHeaviestWeightIndex(int weight1, int weight2, int weight3)
{
var weightToIndexMapping = new Dictionary<int, int> {{weight1, 0}, {weight2, 1}, {weight3, 2}};
return weightToIndexMapping[weightToIndexMapping.Keys.Max()];
}
如果你堅持使用,Sort你可以嘗試使用SortedDictoinary這里的類似:https : //www.geeksforgeeks.org/sorteddictionary-class-in-c-sharp/
uj5u.com熱心網友回復:
沒有任何分配、集合和串列:
using System;
public class Program
{
public static void Main(string[] args)
{
var a = 13;
var b = 14;
var c = 15;
Console.WriteLine(Solve(a, b, c));
}
public static int Solve(int weight0, int weight1, int weight2)
{
return weight0 > weight1
? weight0 > weight2
? 0 : 2
: weight1 > weight2
? 1 : 2;
}
}
請注意,它不會檢查底片。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/351259.html
上一篇:動態變數做空結果
