递归方法求数组最大值

package Array;
import java.util.*;
/**
 * Created by Administrator on 2017/10/14.
 */
public class leftToright {
public int reMax(int a[],int low,int high){
    if(low>high){
        return -1;
    }
    else{
        if(a[low]<reMax(a,low+1,high)){
            return reMax(a,low+1,high);
        }else{
            return a[low];
        }
    }
}
public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] b= new int[n];
    for(int i=0;i<n;i++){
        b[i]=sc.nextInt();
    }
    System.out.println(new leftToright().reMax(b,0,b.length-1));
}

}

猜你喜欢

转载自blog.csdn.net/wuxiaosi808/article/details/78232861