【发布时间】:2011-10-11 15:58:02
【问题描述】:
该方法应该像 Math.Max() 一样工作,但需要 3 个或更多 int 参数。
【问题讨论】:
-
这些数字在哪里?它们是如何存储的?
该方法应该像 Math.Max() 一样工作,但需要 3 个或更多 int 参数。
【问题讨论】:
如果不想重复调用Max函数,可以这样
new List<int>() { A, B, C, D, X, Y, Z }.Max()
【讨论】:
如果出于某种原因(例如 Space Engineers API),System.array 没有 Max 的定义,您也无权访问 Enumerable,Max of n 值的解决方案是:
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;
}
【讨论】:
这个函数接受一个整数数组。 (我完全理解@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
【讨论】:
priceValues[] 中的最大元素值为 maxPriceValues :
double[] priceValues = new double[3];
priceValues [0] = 1;
priceValues [1] = 2;
priceValues [2] = 3;
double maxPriceValues = priceValues.Max();
【讨论】:
你可以试试这个代码:
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;
}
}
【讨论】:
通用
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));
【讨论】:
Linq 有一个Max 函数。
如果您有IEnumerable<int>,您可以直接调用它,但如果您需要在单独的参数中使用它们,您可以创建如下函数:
using System.Linq;
...
static int Max(params int[] numbers)
{
return numbers.Max();
}
然后你可以这样称呼它:max(1, 6, 2),它允许任意数量的参数。
【讨论】:
Max 而不是 max,并使其成为静态 :) 通过重载它以减少参数,你也可以提高效率。
好吧,你可以调用它两次:
int max3 = Math.Max(x, Math.Max(y, z));
如果您发现自己经常这样做,您总是可以编写自己的辅助方法...我会很高兴在我的代码库中看到这一点一次,但不是经常。
(请注意,这可能比 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 方法的 显式 数组创建更具可读性。
【讨论】:
MoreMath.Max(x, y, z) 比 IMO 的 LINQ 方法更具可读性。
public static int Max(params int[] values) ?
Max(1, 2, 3) 将无缘无故地创建一个数组。通过为相对少量的参数提供一些重载,您可以在不影响调用者的可读性的情况下提高效率。
假设你有一个List<int> intList = new List<int>{1,2,3},如果你想获得一个最大值你可以这样做
int maxValue = intList.Max();
【讨论】:
你可以使用Enumerable.Max:
new [] { 1, 2, 3 }.Max();
【讨论】:
[] 创建一个 Enumerable。漂亮。
new int[] { 1,2,3 } 的简写。所以它是一个int类型的数组,由它的内容隐式决定。