我很难从提升形态开始排列数字。
public static int median(int a, int b, int c) {
if (( b < a && a < c) || (b > a && a > c)){
return a;
}
if ((a < b && b < c) || (a > b && b > c)){
return b;
}
return c;
}基本上,当我运行我的代码时,它应该找到这三个数字的中间值,但是当我运行测试代码时,它不会自动按升序排列自己。例如,如果测试代码有数字(3, 6,5 ),它将返回中位数为6,但是它应该是5。
发布于 2022-03-05 12:42:27
要获得中值(基于代码),应该已经对变量a、b&c进行排序。您可以在中值函数中对变量进行排序,例如
public static int median(int a, int b, int c) {
int max = Math.max(a , Math.max(b,c));
int min = Math.min(a , Math.min(b,c));
return a + b + c - max - min;;
}https://stackoverflow.com/questions/71362209
复制相似问题