


Write a recursive function in java to find the maximum value of an array
How to use java to achieve the maximum value of an array recursively
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);
}
How to implement the recursive algorithm of binary search in java
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;
}
}
}
How is Java's recursion executed and how is the order executed?
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
How to use recursion to solve this problem in JAVA? Master
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

This article identifies ene.sys as a Realtek High Definition Audio driver component. It details its function in managing audio hardware, emphasizing its crucial role in audio functionality. The article also guides users on verifying its legitimacy

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

Are you questioned about an issue that MSConfig keeps reverting to selective startup on your Windows? How to switch to normal startup if you require it? Try the methods explained in this php.cn post to find one that works for you.

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.
