Java版本:
public class Main { public static void main(String[] args) { int[] array = {1, 3, 5, 6, 3, 5, 3, 8}; System.out.println(findMaxValue(array)); } private static int findMaxValue(int[] array) { if(array.length <= 0 || array == null) { return -1; } return findMaxValue(array, 0); } private static int findMaxValue(int[] array, int start) { int len = array.length - start; if( len == 1) { return array[start]; } else { return Math.max(array[start], findMaxValue(array, start+1)); } } }
C++版本:
#include<iostream> #include<cmath> using namespace std; int maxnum(int array[], int n) { if(n == 1) { return array[0]; } else { return max(array[0], maxnum(array+1,n-1)); } } int main() { int num[10] = {0,1,2,3,4,5,6,7,8,9}; cout<<maxnum(num,10)<<endl; return 0; }