比较数字进行排序,然后得到中位数 [英] comparing numbers to sort then get median value

查看:117
本文介绍了比较数字进行排序,然后得到中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用按位运算符或比较运算符对五个整数进行排序可以通过首先获得最高的数字,然后获得第二高的数字,然后获得第三的数字,依此类推.

Sorting five integers using bitwise or comparison operators can be achieved by first getting the highest number then the second highest then the third and so on.

这是我获得最高号码的代码:

Here is my code in getting the highest number:

#include <stdio.h>

int main() {
    int a, b, c, d, e;
    int aa, bb, cc, dd, ee;

    a = 4; b = 2; c = 5; d = 1; e = 3;

    aa = (a > b) ?
         ((a > c) ? ((a > d) ? ((a > e) ? a : e) : ((d > e) ? d : e)) :
                    ((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e))) :
         ((b > c) ? ((b > d) ? ((b > e) ? b : e) : ((d > e) ? d : e)) :
                    ((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e)));


    printf("highest: %d\n", aa);
    return 0;
}

我认为使用此方法可以获得第二,第三,第四和第五高的数字.

I think getting the second, third, forth and fifth highest number could be possible using this method.

是否还有其他方法可以使用比较/按位运算符获取五个整数的中位数?任何其他组合方法都可能有效.

Is there any other way in getting the median of five integers using comparison/bitwise operators? any other combinatorial method might be valid.

顺便说一句,我将在硬件中实现此算法.

By the way, I'm going to implement this algorithm in hardware.

在组合中使用组合方法将很快,而不是使用状态机.

Using combinatorial method in sorting will be fast rather than using a state machine.

推荐答案

一种考虑方法是将5个数字之间的10个比较操作视为二进制输入.然后您可以选择:

One way to think about it is to consider the 10 comparison operations between the 5 numbers as your binary inputs. Then you have options:

  1. 绘制10个输入的卡诺图,并在可能的情况下简化逻辑.
  2. 构建一个10位数字作为表索引.

某些可能性永远不会发生,所以我敢肯定有一些简化的可能性.例如,如果(a> b)和(b> c),则(a> c)将始终为true.这将有助于方法1,并在方法2中产生错误情况.

Some of the possibilities will never occur, so I'm sure there's some simplification possible. For instance if (a>b) and (b>c) then (a>c) will always be true. Which will help with approach #1 and generates an error case in approach #2.

这篇关于比较数字进行排序,然后得到中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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