Java 三个数中的第二个最大数,不使用if-else或三元运算符。使用max和min函数
上述代码用于使用最大值和最小值函数查找3个数字中的第二个最大值。没有if-else或三元运算符,只需使用max和min函数,就可以找到任何快捷代码。获得第二个max的简单解决方案Java 三个数中的第二个最大数,不使用if-else或三元运算符。使用max和min函数,java,arrays,Java,Arrays,上述代码用于使用最大值和最小值函数查找3个数字中的第二个最大值。没有if-else或三元运算符,只需使用max和min函数,就可以找到任何快捷代码。获得第二个max的简单解决方案 void cal(int a,int b,int c) { int x=Math.max(a,b); int y=Math.max(b,c); int z=Math.max(a,c); int z1=Math.max(x,y); int
void cal(int a,int b,int c)
{
int x=Math.max(a,b);
int y=Math.max(b,c);
int z=Math.max(a,c);
int z1=Math.max(x,y);
int z2=Math.max(z1,z);
System.out.print("2nd max"+z2)
}
我们想找出中间的元素。因此,我们找到所有三个值的最大值和最小值:
a+b+c - Math.max(Math.max(a,b),c)- Math.min(Math.min(a,b),c)
我们可以通过找出所有元素与最小值和最大值之间的差值来获得中间元素,即缺失元素。
int maximum = Math.max(Math.max(a,b),c)
int minimum = Math.min(Math.min(a,b),c)
<代码>差异< /代码>现在是中间元素,第二个最大值。
< P>简单的解决方案得到第二个最大值。void cal(int a,int b,int c)
{
int x=Math.max(a,b);
int y=Math.max(b,c);
int z=Math.max(a,c);
int z1=Math.max(x,y);
int z2=Math.max(z1,z);
System.out.print("2nd max"+z2)
}
int maximum = Math.max(Math.max(a,b),c)
int minimum = Math.min(Math.min(a,b),c)
我们想找出中间的元素。因此,我们找到所有三个值的最大值和最小值:
a+b+c - Math.max(Math.max(a,b),c)- Math.min(Math.min(a,b),c)
我们可以通过找出所有元素与最小值和最大值之间的差值来获得中间元素,即缺失元素。
int maximum = Math.max(Math.max(a,b),c)
int minimum = Math.min(Math.min(a,b),c)
<代码>差异现在是中间元素,第二个最大值。
< P>添加数组中的元素,然后对数组排序。在您的情况下,类似于:int maximum = Math.max(Math.max(a,b),c)
int minimum = Math.min(Math.min(a,b),c)
int allElements = a + b + c;
int minAndMax = maximum + minimum;
int difference = allElements - minAndMax
在数组中添加元素,然后对数组进行排序。在您的情况下,类似于:
int allElements = a + b + c;
int minAndMax = maximum + minimum;
int difference = allElements - minAndMax
您可以通过过滤第一个流,在第一个流中识别最大值,在第二个流中识别第二个最大值:
Integer[] array = {a,b,c};
Arrays.sort(array);
return array[array.length-2];
void-cal(内部a、内部b、内部c){
最终int[]值=新的int[]{a,b,c};
int max=Arrays.stream(值)
.max().getAsInt();
int secondMax=Arrays.stream(值)
.filter(i->i
您可以通过过滤第一个流,在第一个流中识别最大值,在第二个流中识别第二个最大值:
Integer[] array = {a,b,c};
Arrays.sort(array);
return array[array.length-2];
void-cal(内部a、内部b、内部c){
最终int[]值=新的int[]{a,b,c};
int max=Arrays.stream(值)
.max().getAsInt();
int secondMax=Arrays.stream(值)
.filter(i->i
您可以找到最大值,将其删除,然后再次找到。这种方法的优点是它也适用于任意多个输入
void cal(int a,int b,int c){
final int[] values = new int[]{a, b,c};
int max = Arrays.stream(values)
.max().getAsInt();
int secondMax = Arrays.stream(values)
.filter(i -> i < max).max().getAsInt();
}
有了Java8,您还可以使用
流
,它们的优点是可以轻松地并行执行。但我怀疑这是否有助于对3个元素进行排序。。。无论如何,这里有一个片段:
void cal(int a, int b, int c) {
int[] values = new int[] {a, b, c};
// Sorts in-place using efficient sorting algorithms
Arrays.sort(values);
// The first element is now the smallest, the last the biggest,
// we take the element in the middle
System.out.print("2nd max" + values[1]);
}
您可以找到最大值,删除它,然后再次找到它。这种方法的优点是它也适用于任意多个输入
void cal(int a,int b,int c){
final int[] values = new int[]{a, b,c};
int max = Arrays.stream(values)
.max().getAsInt();
int secondMax = Arrays.stream(values)
.filter(i -> i < max).max().getAsInt();
}
有了Java8,您还可以使用
流
,它们的优点是可以轻松地并行执行。但我怀疑这是否有助于对3个元素进行排序。。。无论如何,这里有一个片段:
void cal(int a, int b, int c) {
int[] values = new int[] {a, b, c};
// Sorts in-place using efficient sorting algorithms
Arrays.sort(values);
// The first element is now the smallest, the last the biggest,
// we take the element in the middle
System.out.print("2nd max" + values[1]);
}
首先,您应该注意JavaAPI
Math.max
该方法还使用if
检查来查找最大值
您可以对三个int
值进行流式处理,以进行排序并找到第二个最大值,如下所示:
void cal(int a, int b, int c) {
int value = IntStream.of(a, b, c).sorted().toArray()[1];
System.out.print("2nd max" + value);
}
首先,您应该注意JavaAPI
Math.max
该方法还使用if
检查来查找最大值
您可以对三个int
值进行流式处理,以进行排序并找到第二个最大值,如下所示:
void cal(int a, int b, int c) {
int value = IntStream.of(a, b, c).sorted().toArray()[1];
System.out.print("2nd max" + value);
}
添加到
列表中
并删除最大值和最小值
void cal(int a, int b, int c) {
int secondMax = Arrays.asList(a, b, c).stream().
sorted().mapToInt(num -> num).toArray()[1];
System.out.println(secondMax);
}
static int secondGreatestNumber(int a、int b、int c)
{
列表编号=新的ArrayList();
增加(a);
增加(b);
增加(c);
numbers.remove(新整数(Math.max(a,Math.max(b,c));//删除最大值
numbers.remove(新整数(Math.min(a,Math.min(b,c)));//删除最小值
返回数字。获取(0);
}
添加到列表
并删除最大值和最小值
void cal(int a, int b, int c) {
int secondMax = Arrays.asList(a, b, c).stream().
sorted().mapToInt(num -> num).toArray()[1];
System.out.println(secondMax);
}
static int secondGreatestNumber(int a、int b、int c)
{
列表编号=新的ArrayList();
增加(a);
增加(b);
增加(c);
numbers.remove(新整数(Math.max(a,Math.max(b,c));//删除最大值
numbers.remove(新整数(Math.min(a,Math.min(b,c)));//删除最小值
返回数字。获取(0);
}
no array no loop no if else no only max and min function no array no loop no if else no only max and min function您似乎只对一个完全数学化的max/min only解决方案感兴趣。在这种情况下,你应该在你的问题中清楚地说明这一点。在当前的答案中,您可以看到大多数人都试图通过向您展示Java API的一些简洁的函数来帮助您,而不是像@nagendra547那样发布纯数学表达式,因为您没有清楚地表达您想要的。是的,很抱歉,如果用户回答了您的问题,请也接受他的答案()。如果不超过,请说明还有什么问题没有回答,这是StackOverflow的一个非常关键的部分,非常感谢。您似乎只对一个完全数学化的仅限最大/最小值的解决方案感兴趣。在这种情况下,你应该在你的问题中清楚地说明这一点。在当前的答案中,您可以看到大多数人都试图通过向您展示Java API的一些简洁的函数来帮助您,而不是像@nagendra547那样发布纯数学表达式,因为您没有清楚地表达您想要的。是的,很抱歉,如果用户回答了您的问题,请也接受他的答案()。如果不超过,请说明还有什么问题没有回答,这是StackOverflow的一个非常重要的部分,非常感谢。@Zabuza,因为a+b+c是总和。max的第一项max是所有三项中的max。第二项是min of min,这是三项中的min。所以三者之和减去最大值和最小值必须等于中间值@阿披实达如果你认为,我的回答回答了你的问题,你可以接受。要将答案标记为已接受,请单击答案旁边的复选标记,将其从灰色切换为已填写。:)@因为a+b+c是总和。max的第一项max是所有三项中的max。第二项是min of min,这是三项中的min。所以求和