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 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services