Home >Computer Tutorials >Computer Knowledge >Write a recursive function in java to find the maximum value of an array
public static void main(String[] rags){
int [] aim = new int[100];
int point = 0;
//....Initialize the array here
int max = aim[0];
max = getMax(max,point,aim);
//...Other processing
}
//Recursive method
public int getMax(int max,int point,int[] aim){
if(point==aim.length) //Critical value
return max;
//When the critical value is not reached, take the max value and perform recursion
max = max >= aim[point] ? max : aim[point];
return getMax(max,point 1,aim);
}
public class Binary recursive search {
public static void main(String[] args) {
//Define the array. Note that the binary search array must be an ordered array!
int[] arr = { 1, 3, 5, 7, 9, 11, 13, 15, 17 };
//Accept the return value after the search: index value, if not, it is -1;
//Test search element: 9
int a=binary(arr, 9, 0, arr.length - 1);
System.out.println("The index position of the number being searched is:" a);
}
//The parameter list is: the array to be searched, the number to search for, the head index, and the tail index!
public static int binary(int[] arr, int key, int star, int end)//recursion
{
//Create every time you come in, the intermediate index value!
int mid = (star end) / 2;
//If the number being searched is less than the head or tail, or the head index is greater than the tail index, it means there is no such number and -1 is returned;
if (key arr[end] || star > end) {
return -1;
}
//If the middle value is less than the number being searched, redefine the header index and move it to the middle 1 position, filtering out half of the numbers!
if (arr[mid]
//Start recursion!
return binary(arr, key, mid 1, end);
//Otherwise, if the middle value is greater than the number being searched, the tail index will be moved to the middle -1 position and half of the numbers will be filtered out!
} else if (arr[mid] > key) {
//Start recursion!
return binary(arr,key, star, mid - 1);
} else {
//If not, it is found and returns to the index!
return mid;
}
}
}
factest(8) enters the factest function, if(n==1) return 1; // If not established, execute else else return n*factest(n-1); // The return value is 8*factest(7)
factest(7) enters the factest function, if(n==1) return 1; // If not established, execute else
else return n*factest(n-1); // The return value is 7*factest(6)
……
Until N=1, at this time if(n==1) return 1; // Established, the return value is 1, that is, 1!=1
Then calculate the return value of factest(2) as: 2*factest(1) = 2
Then continue to calculate the return value of factest(3): 3*factest(2) = 6
...... Until N=8, get factest(8) = 8*factest(7) = 40320
The Java recursive program you want to write is as follows:
import java.util.Scanner;
public class GGG {
public static void main(String[] args) {
int N = 0;
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
for(int n=0;n
N=sc.nextInt();
int a[]=new int[N];
for(int i=0;i
a[i]=sc.nextInt();
}
System.out.print("case" (n 1) ":");
process(a,0);
System.out.println();
}
}
private static void process(int[] a, int n) {
if(n==0){
if(isPrime(a[n 1]))
System.out.print(1 " ");
else
System.out.print(0 " ");
}else if(n==a.length-1){
if(isPrime(a[n-1]))
System.out.print(1 " ");
else
System.out.print(0 " ");
return;
}else{
if(isPrime(a[n-1])&isPrime(a[n 1]))
System.out.print(2 " ");
else if(isPrime(a[n-1])||isPrime(a[n 1]))
System.out.print(1 " ");
else
System.out.print(0 " ");
}
process(a,n 1);
}
public static boolean isPrime(int num) {
int i;
for(i=2;i
if(num%i==0)
break;
}
if(i==num){
return true;
}
return false;
}
}operation result:
2
5
5 7 2 9 13
case 1:1 2 1 2 0
3
10 4 5
case 2:0 1 0
The above is the detailed content of Write a recursive function in java to find the maximum value of an array. For more information, please follow other related articles on the PHP Chinese website!