Come Math.Max ma richiede 3 o parametri di int?
Grazie
Come Math.Max ma richiede 3 o parametri di int?
Grazie
Risposte:
Bene, puoi chiamarlo due volte:
int max3 = Math.Max(x, Math.Max(y, z));
Se ti accorgi di farlo spesso, potresti sempre scrivere il tuo metodo di supporto ... Sarei abbastanza felice di vederlo nel mio codice una volta , ma non regolarmente.
(Si noti che è probabile che questo sia più efficiente della risposta basata su LINQ di Andrew, ma ovviamente più elementi si hanno più è interessante l'approccio LINQ.)
EDIT: un approccio "il meglio di entrambi i mondi" potrebbe essere quello di avere un set personalizzato di metodi in entrambi i casi:
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);
}
}
In questo modo puoi scrivere MoreMath.Max(1, 2, 3)o MoreMath.Max(1, 2, 3, 4)senza l'overhead della creazione dell'array, ma MoreMath.Max(1, 2, 3, 4, 5, 6)puoi comunque scrivere per un codice leggibile e coerente quando non ti dispiace l'overhead.
Personalmente lo trovo più leggibile rispetto alla creazione esplicita di array dell'approccio LINQ.
MoreMath.Max(x, y, z)è ancora più leggibile dell'approccio LINQ, IMO.
public static int Max(params int[] values) ?
Max(1, 2, 3)creerà un array senza motivo. Fornendo alcuni overload per un numero relativamente ridotto di parametri, è possibile renderlo più efficiente senza influire sulla leggibilità del chiamante.
Potresti usare Enumerable.Max:
new [] { 1, 2, 3 }.Max();
[]. Nifty.
new int[] { 1,2,3 }. Quindi è un array di tipo int, che è implicitamente determinato dal suo contenuto.
Linq ha una funzione Max .
Se hai un IEnumerable<int>puoi chiamarlo direttamente, ma se li richiedi in parametri separati potresti creare una funzione come questa:
using System.Linq;
...
static int Max(params int[] numbers)
{
return numbers.Max();
}
Quindi potresti chiamarlo in questo max(1, 6, 2)modo:, consente un numero arbitrario di parametri.
Maxpiuttosto che max, e renderlo statico :) Sovraccaricandolo per meno parametri, puoi anche renderlo più efficiente.
Come generico
public static T Min<T>(params T[] values) {
return values.Min();
}
public static T Max<T>(params T[] values) {
return values.Max();
}
fuori tema ma ecco la formula per il valore medio .. nel caso qualcuno lo stia cercando
Math.Min(Math.Min(Math.Max(x,y), Math.Max(y,z)), Math.Max(x,z));
Se, per qualsiasi motivo (ad es. API Space Engineers), System.array non ha una definizione per Max né hai accesso a Enumerable, una soluzione per Max di n valori è:
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;
}
Il valore massimo dell'elemento in priceValues [] è maxPriceValues:
double[] priceValues = new double[3];
priceValues [0] = 1;
priceValues [1] = 2;
priceValues [2] = 3;
double maxPriceValues = priceValues.Max();
Questa funzione accetta un array di numeri interi. (Comprendo perfettamente il reclamo di @Jon Skeet sull'invio di array.)
Probabilmente è un po 'eccessivo.
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;
}
Quindi chiama la funzione:
int highest_val = GetMax (new[] { 1,6,2,72727275,2323});
// highest_val = 72727275