0
点赞
收藏
分享

微信扫一扫

用一个方法查出宜个数值类型数组的最大值,用递归方式实现

jjt二向箔 2022-02-11 阅读 168
//方法 1
public class Test1 {
public static int a(int[] i,int j){
if(i.length-1>j){
if(i[j]>i[j+1]){
i[j+1]=i[j];
}
return a(i,j+1);
}else{
return i[i.length-1];
} } }
// 方法 2 -- 非递归
public static int test(int []num) { int x=0;
int log =
num.Length;for(intt=0;t<log;t++){ if(num[t]>x){ x=num[t]; } }return
x;}
// 方法 3 --- 递归 不改变原数组中的元素
public static int getMax(int[]a, int index,int max){
int len = a.length;
if(len==1){
return a[len-1];
}
if(index==0){
max = a[index];
}
if(index==len){
return max;
}
if(max<a[index]){
max = a[index];
}
index++;
return getMax(a,index,max);
}
// 测试
int max = getMax(new int[]{2,5,18,3,38,10,2},0,0);
System.out.println(max);
举报

相关推荐

0 条评论