


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!

Do you have trouble downloading or sending attachments in Outlook 365? Sometimes, Outlook doesn’t show them for some unknown reason, so you are unable to see them. In this post on php.cn Website, we collect some use tips for attachments not showing.

When V Rising players try to join a server that is close to or already full, they may encounter the “V Rising connection timed out” issue. If you are one of them, you can refer to this post from php.cn to get solutions. Now, keep on your reading.

Windows supplies real-time protection via Windows Security. But this feature may prevent you from doing something it thinks are dangerous. In this situation, you may want to temporarily turn on real-time protection. This php.cn post will show you how

Microsoft has started working on next year’s Windows updates very early. Recent rumors state that the next update in 2024 might be Windows 11 24H2 rather than Windows 12. Everything is uncertain now. php.cn will now take you to see some related infor

The error 0x80030001 often happens when you are attempting to copy files. The error code will be accompanied by a message that tells “unable to perform requested operation”. If you are struggling with this error, you can read this article on php.cn W

On February 13, 2024, Microsoft released KB5034765 (OS builds 22621.3155 and 22631.3155) for Windows 11 22H2 and Windows 11 23H2. This security update brings you many new improvements and bug fixes. You can learn how to download and install Windows 1

Device Manager is widely used when you need to fix some computer issues. You can check the problematic devices and decide to uninstall or update device drivers. Besides, you can also set Power Management settings in Device Manager. However, you may f

When Backup and Restore (Windows Backup) fails to work, you can choose to reset it to default. How to restore Windows Backup to default in Windows 11/10? php.cn will guide you to easily do this thing in 2 ways and let’s go to see them.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools