IT

C #에서 최대 3 개의 숫자를 찾는 방법이 있습니까?

lottoking 2020. 9. 17. 08:05
반응형

C #에서 최대 3 개의 숫자를 찾는 방법이 있습니까?


Math.Max와 미래하지만 int의 3 개 또는 매개 변수를 사용합니까?

감사합니다


글쎄, 당신은 그것을 두 번 호출 할 수 있습니다.

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);
    }
}

하면 이렇게 배열 생성의 over-헤드없이 작성 MoreMath.Max(1, 2, 3)하거나 작성할 수 MoreMath.Max(1, 2, 3, 4)있지만 MoreMath.Max(1, 2, 3, 4, 5, 6)over-헤드에 신경 쓰지 않아도 읽기 쉽고 일관된 코드를 작성할 수 있습니다.

개인적 으로 LINQ 방식 접근 명시 적 배열 생성 보다 더 읽기 쉽습니다.


다음을 사용할 수 있습니다 .Enumerable.Max

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

Linq에는 Max 기능이 있습니다.

가있는 경우 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));

하자 당신은이 가정에서 List<int> intList = new List<int>{1,2,3}당신은 당신이 할 수있는 값을 얻으려면

int maxValue = intList.Max();

어떤 이유로 든 (예 : Space Engineers API) System.array에 Max에 대한 정의가 없거나 Enumerable에 대한 액세스 권한이없는 경우 n의 Max (또는 Min)에 대한 솔루션 은 다음과 같습니다.

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;
    }
}

priceValues ​​[]의 최대 요소 값은 maxPriceValues입니다.

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

참고 URL : https://stackoverflow.com/questions/6800838/in-c-sharp-is-there-a-method-to-find-the-max-of-3-numbers

반응형