yi4521870 2013-12-03 16:07
浏览 444
已采纳

求一个数组中任意几个下标中最大值的下标

下面三个方法是分别求数组任意两个/三个/四个下标中最大值下标。问题来了,求一个数组任意个下标中最大值下标是多少。应该怎么处理呢?请别吐槽什么实用性在哪什么的,欢迎各位提出自己的见解。

/**
 * 求数组中任意两个下标的中最大值下标 
 */
public static int simmax2(char x[], int a, int b) {
    int l = x.length ;
    if( l < a-1 || l < b-1 )throw new ArrayIndexOutOfBoundsException("method simmax2 ArrayIndexOutOfBoundsException ") ;
    return (x[a] > x[b] ? a : b);
}
/**
 * 求数组中任意三个下标的中最大值下标 
 */
public static int simmax3(char x[], int a, int b, int c) {
    int l = x.length ;
    if( l < a-1 || l < b-1 || l < c-1 )throw new ArrayIndexOutOfBoundsException("method simmax3 ArrayIndexOutOfBoundsException ") ;
    return (x[a] > x[b] ? (x[b] > x[c] ? a : x[a] > x[c] ? a : c) : ( x[b] > x[c] ? b : c));
}
/**
 * 求数组中任意四个下标的中最大值下标 
 */
public static int simmax4( char x[], int a, int b, int c, int d){
    int l = x.length ;
    if( l < a-1 || l < b-1 || l < c-1 || l < d-1 )throw new ArrayIndexOutOfBoundsException("method simmax4 ArrayIndexOutOfBoundsException ") ;
    return  (x[a] > x[b]) ? (x[c] > x[d] ? (x[a] > x[c] ? a : c ) : (x[a] > x[d] ? a : d)) : (x[c]>x[d] ? (x[b] > x[c] ? b : c) : ( x[b] > x[d] ? b : d)) ;
}
  • 写回答

2条回答 默认 最新

  • weixin_42405879 2013-12-03 16:45
    关注

    [code="java"]
    public static int simmax(char x[], int... indexes) {
    int indexOfMax=indexes[0];
    for(int n=1;n if(x[indexes[n]]>x[indexOfMax])
    indexOfMax = indexes[n];
    return indexOfMax;
    }
    [/code]
    弄个变量保存最大值就OK了,你能用三目运算符写出这么复杂的表达式,怎么就不会转下弯呢?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题