有没有一种方法可以找到最大的3个数字在 C # ?

这个方法应该像 Math.Max()一样工作,但是需要3个或更多的 int参数。

144450 次浏览

你可以说两遍:

int max3 = Math.Max(x, Math.Max(y, z));

如果您发现自己经常这样做,那么您总是可以编写自己的 helper 方法... ... 我很高兴在我的代码库 一次中看到这一点,但不是经常看到。

(请注意,这可能比 Andrew 的基于 LINQ 的答案更有效率——但显然,你拥有的元素越多,LINQ 方法就越有吸引力。)

编辑: 一个“两全其美”的方法可能是有一套自定义的方法:

public static class MoreMath
{
// This method only exists for consistency, so you can *always* call
// MoreMath.Max instead of alternating between MoreMath.Max and Math.Max
// depending on your argument count.
public static int Max(int x, int y)
{
return Math.Max(x, y);
}


public static int Max(int x, int y, int z)
{
// Or inline it as x < y ? (y < z ? z : y) : (x < z ? z : x);
// Time it before micro-optimizing though!
return Math.Max(x, Math.Max(y, z));
}


public static int Max(int w, int x, int y, int z)
{
return Math.Max(w, Math.Max(x, Math.Max(y, z)));
}


public static int Max(params int[] values)
{
return Enumerable.Max(values);
}
}

这样,您可以编写 MoreMath.Max(1, 2, 3)MoreMath.Max(1, 2, 3, 4)而不用承担数组创建的开销,但是当您不介意开销时,仍然可以编写具有良好可读性和一致性的 MoreMath.Max(1, 2, 3, 4, 5, 6)代码。

我个人发现这比创建 露骨数组的 LINQ 方法更具可读性。

你可以使用 Enumerable.Max:

new [] { 1, 2, 3 }.Max();

让我们假设您有一个 List<int> intList = new List<int>{1,2,3},如果您想得到一个最大值,您可以这样做

int maxValue = intList.Max();

Linq 有一个 麦克斯函数。

如果你有一个 IEnumerable<int>,你可以直接调用它,但是如果你需要在单独的参数中使用它,你可以创建一个像下面这样的函数:

using System.Linq;


...


static int Max(params int[] numbers)
{
return numbers.Max();
}

然后您可以这样调用它: max(1, 6, 2),它允许任意数量的参数。

很普通

public static T Min<T>(params T[] values) {
return values.Min();
}


public static T Max<T>(params T[] values) {
return values.Max();
}

偏离主题,但这里是中等价值的公式. . 以防有人正在寻找它

Math.Min(Math.Min(Math.Max(x,y), Math.Max(y,z)), Math.Max(x,z));

如果由于某种原因(例如 Space Engineers API) ,System.array 没有 Max 的定义,也不能访问 Enumable,那么 N值的 Max 的解决方案是:

public int Max(int[] values) {
if(values.Length < 1) {
return 0;
}
if(values.Length < 2) {
return values[0];
}
if(values.Length < 3) {
return Math.Max(values[0], values[1]);
}
int runningMax = values[0];
for(int i=1; i<values.Length - 1; i++) {
runningMax = Math.Max(runningMax, values[i]);
}
return runningMax;
}

你可以试试这个代码:

private float GetBrightestColor(float r, float g, float b) {
if (r > g && r > b) {
return r;
} else if (g > r && g > b) {
return g;
} else if (b > r && b > g) {
return b;
}
}

PriceValue []中的最大元素值是 maxPriceValue:

double[] priceValues = new double[3];
priceValues [0] = 1;
priceValues [1] = 2;
priceValues [2] = 3;
double maxPriceValues = priceValues.Max();

这个函数接受一个整数数组(我完全理解@Jon Skeet 关于发送数组的抱怨)

可能有点过了。

    public static int GetMax(int[] array) // must be a array of ints
{
int current_greatest_value = array[0]; // initializes it


for (int i = 1; i <= array.Length; i++)
{
// compare current number against next number


if (i+1 <= array.Length-1) // prevent "index outside bounds of array" error below with array[i+1]
{
// array[i+1] exists
if (array[i] < array[i+1] || array[i] <= current_greatest_value)
{
// current val is less than next, and less than the current greatest val, so go to next iteration
continue;
}
} else
{
// array[i+1] doesn't exist, we are at the last element
if (array[i] > current_greatest_value)
{
// current iteration val is greater than current_greatest_value
current_greatest_value = array[i];
}
break; // next for loop i index will be invalid
}


// if it gets here, current val is greater than next, so for now assign that value to greatest_value
current_greatest_value = array[i];
}


return current_greatest_value;
}

然后调用函数:

int highest_val = GetMax (new[] { 1,6,2,72727275,2323});


// highest_val = 72727275

如果不想重复调用 Max 函数,可以这样做

new List<int>() { A, B, C, D, X, Y, Z }.Max()

以防你也需要分类:

var side = new double[] {5,3,4}
Array.Sort(side);


//side[2] is a maximum

作为另一种变体:

T[] GetMax<T>(int number, List<T> source, T minVal)
{
T[] results = new T[number];


for (int i = 0; i < number; i++)
{
results[i] = minVal;
}


var curMin = minVal;


foreach (var e in source)
{
int resComp = Comparer.DefaultInvariant.Compare(curMin, e);


if (resComp < 0)
{
int minIndex = Array.IndexOf(results, curMin);
results[minIndex] = e;
curMin = results.Min();
}
}


return results;
}


var source = new int[] { 5, 5, 1, 2, 4, 3 }.ToList();
var result = GetMax(3, source, int.MinValue);

对于三个值,可以使用 if 和 else if 方法,但是如果调用两次 Math 则会简单得多。像这样的 Max 方法

        Console.WriteLine("Largest of three: " + Math.Max(num1, Math.Max(num2, num3)));
Console.WriteLine("Lowest of three: " + Math.Min(num1, Math.Min(num2, num3)));