比较数字进行排序,然后得到中位数

编程入门 行业动态 更新时间:2024-10-23 06:33:22
本文介绍了比较数字进行排序,然后得到中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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:

  • 绘制10个输入的卡诺图,并在可能的情况下简化逻辑.
  • 构建一个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.

    更多推荐

    比较数字进行排序,然后得到中位数

    本文发布于:2023-11-29 16:39:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646969.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中位数   数字

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!