使用按位运算符或比较运算符对五个整数进行排序可以通过首先获得最高的数字,然后获得第二高的数字,然后获得第三的数字,依此类推.
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:
某些可能性永远不会发生,所以我敢肯定有一些简化的可能性.例如,如果(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.
更多推荐
比较数字进行排序,然后得到中位数
发布评论