code来计算"五&QUOT值;在C# [英] Code to calculate "median of five" in C#

查看:116
本文介绍了code来计算"五&QUOT值;在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:请不要跨preT这是家庭作业的问题。这仅仅是一个事情,我好奇地想知道:)

Note: Please don't interpret this as "homework question." This is just a thing I curious to know :)

五的中位数有时被用来作为一个练习在算法设计和被称为是可计算的只用6比较

The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons.

什么是实现这个五个用6位数的比较的最好办法在C#?我所有的努力似乎导致尴尬的code :(我需要很好的和可读code,同时仍然只用6比较。

What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem to result in awkward code :( I need nice and readable code while still using only 6 comparisons.

public double medianOfFive(double a, double b, double c, double d, double e){
	//
	// return median
	//
	return c;
}

注意:我觉得我应该提供的算法这里太:

Note: I think I should provide the "algorithm" here too:

我发现自己无法解释清楚的算法相同的 Azereal 的在他的论坛上发帖做。所以,我将在这里引用他的职位。从<一个href="http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085">http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085

I found myself not able to explain the algorithm clearly as Azereal did in his forum post. So I will reference his post here. From http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085

嗯,我提出一个这个问题   我的任务,而我变成这样   论坛寻求帮助,但没有帮助在这里。   我终于找到了如何做到这一点。

Well I was posed this problem in one of my assignments and I turned to this forum for help but no help was here. I eventually found out how to do it.

      
  1. 先说第4个元素和秩序每对一个合并(2   比较)

  1. Start a mergesort with the first 4 elements and order each pair (2 comparisons)

比较每一对的两个下部的并消除最低的一个,从   准备(3比较)

Compare the two lower ones of each pair and eliminate the lowest one from the possibilities (3 comparisons)

添加在5号预留的数字,没有对和比较   在两(4比较)

Add in the 5th number set aside to the number without a pair and compare the two (4 comparisons)

比较两个最低的两对新的,淘汰的较低者   (5比较)

Compare the two lowest of the two new pairs and eliminate the lower one (5 comparisons)

比较所述一个本身和最后一对的下部和下   号是中值

Compare the one by itself and the lower of the last pair and the lower number is the median

可能的中间距离   parentesis

The possible median is within the parentesis

(54321)

5:4的3:2 2比较

5:4 3:2 2 comparisons

(4℃5 2 3; 1)

(4<5 2<3 1)

4:2 3比较

2(4℃5 3 1)

2(4<5 3 1)

1:3 4比较

2(4℃5 1 3;)

2(4<5 1<3)

4:1 5的比较

1,2(4℃; 5 3)

1,2(4<5 3)

4:3 6比较

1,2(3)4,5

1,2(3)4,5

三是中位数

编辑:作为您的要求,以prevent自己从越来越downvotes,这是C ++ code我写找到五个位数。不介意的尴尬:

As your request and to prevent myself from getting more downvotes, this are C++ code I wrote to find median of five. Don't mind it's awkwardness:

double StageGenerator::MedianOfFive(double n1, double n2, double n3, double n4, double n5){
    double *a = &n1, *b = &n2, *c = &n3, *d = &n4, *e = &n5;
    double *tmp;

    // makes a < b and b < d
    if(*b < *a){
    	tmp = a; a = b; b = tmp;
    }

    if(*d < *c){
    	tmp = c; c = d; d = tmp;
    }

    // eleminate the lowest
    if(*c < *a){
    	tmp = b; b = d; d = tmp; 
    	c = a;
    }

    // gets e in
    a = e;

    // makes a < b and b < d
    if(*b < *a){
    	tmp = a; a = b; b = tmp;
    }

    // eliminate another lowest
    // remaing: a,b,d
    if(*a < *c){
    	tmp = b; b = d; d = tmp; 
    	a = c;
    }

    if(*d < *a)
    	return *d;
    else
    	return *a;

}

这应该是更紧凑,不是吗?

It should be more compact, isn't it ?

编辑:

由于@pablito指出,在他的回答。内置List.Sort()不能满足这一要求,因为它使用多达13比较:]

As @pablito pointed out in his answer. The built-in List.Sort() cannot fulfill this requirement since it uses up to 13 comparisons :]

推荐答案

这基本上是分解出的交换和排序code从C ++的例子:

This is basically just factoring out the swapping and sorting code from your C++ example:

private static void Swap(ref double a, ref double b) {
    double t = a;
    a = b;
    b = t;
}

private static void Sort(ref double a, ref double b) {
    if (a > b) {
        double t = a;
        a = b;
        b = t;
    }
}

private static double MedianOfFive(double a, double b, double c, double d, double e){
    // makes a < b and c < d
    Sort(ref a, ref b);
    Sort(ref c, ref d);

    // eleminate the lowest
    if (c < a) {
        Swap(ref b, ref d);
        c = a;
    }

    // gets e in
    a = e;

    // makes a < b
    Sort(ref a, ref b);

    // eliminate another lowest
    // remaing: a,b,d
    if (a < c) {
        Swap(ref b, ref d);
        a = c;
    }

    return Math.Min(d, a);
}

这篇关于code来计算&QUOT;五&QUOT值;在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆