Recursive summary--day20

Source: Internet
Author: User

recursion: The phenomenon of invoking the method itself in a method definition

Recursion is actually the loop call method itself, divided into recursive with parameters, with no parameters recursive. Similar to a set of dolls.

The nested invocation of the method, which is not recursive.
Math.max (Math.max (A, B), c);

public void Show (int n) {
if (n <= 0) {
System.exit (0);
}
SYSTEM.OUT.PRINTLN (n);
Show (--N);
}

Precautions:
A: recursion must have an exit, otherwise it is dead recursion
B: The number of recursion can not be too many, otherwise memory overflow
C: Construction method cannot be used recursively

Example:
A: There used to be a mountain, there was a temple in the mountain, there was an old monk and a little monk in the temple, the old Monk told the story to the Little Monk, the story is:
There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is:
There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is:
There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is:
...
The temple's hung up, or a landslide.
B: Learn programming--high-paying employment--earning money--marrying a daughter-in-law------------earn tuition
Learning programming--high-paying employment--earning money--marrying a daughter-in-law--a doll--herding sheep--earning tuition
Learning programming--high-paying employment--earning money--marrying a daughter-in-law--a doll--herding sheep--earning tuition
Learning programming--high-paying employment--earning money--marrying a daughter-in-law--a doll--herding sheep--earning tuition
...
You can't marry a wife or have a baby.


/*
* Requirements: Please use code to achieve the factorial of 5.
* The following knowledge to know:
* 5! = 1*2*3*4*5
* 5! = 5*4!
*
* How many schemes do you have to implement?
* A: Loop implementation
* B: Recursive implementation
* A: Do recursion to write a method
* B: Export conditions
* C: Law
*/
public class Diguidemo {
public static void Main (string[] args) {
int JC = 1;
for (int x = 2; x <= 5; × x + +) {
JC *= x;
}
System.out.println ("5 factorial is:" + JC);

System.out.println (The factorial of "5" is: "+jiecheng (5)");
}

/*
* Do recursion to write a method:
* Return value type: int
* parameter list: int n
* Export conditions:
* if (n = = 1) {return 1;}
Law
* IF (n! = 1) {return n method name (n-1);}
*/
public static int Jiecheng (int n) {
if (n==1) {
return 1;
}else {
Return N*jiecheng (n-1);
}
}
}

Recursive method = Export condition + recursion rule;


/*
* There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the 20th month of the number of rabbits?
* Analysis: We need to find a way to rule
* Rabbit logarithm
* First Month: 1
* Second Month: 1
* Third Month: 2
* Fourth Month: 3
* Fifth Month: 5
* Sixth Month: 8
* ...
*
* This shows that the data of the Rabbit object is:
* 1,1,2,3,5,8 ...
Rules
* A: Starting from the third item, each item is the sum of the first two items
* B: And stating that the first two items are known
*
* How to implement this program?
* A: Array implementation
* B: Change of variable realization
* C: Recursive implementation
*
* If the two-month-old rabbit logarithm is a, b
* First adjacent data: a=1,b=1
* Second adjacent data: a=1,b=2
* Third adjacent data: a=2,b=3
* Fourth adjacent data: a=3,b=5
* See: The next time A is the previous B, the next one is the previous a+b
*/
public class DiGuiDemo2 {
public static void Main (string[] args) {
Define an array
int[] arr = new INT[20];
Arr[0] = 1;
ARR[1] = 1;
ARR[2] = arr[0] + arr[1];
ARR[3] = arr[1] + arr[2];
// ...
for (int x = 2; x < arr.length; × x + +) {
ARR[X] = Arr[x-2] + arr[x-1];
}
System.out.println (arr[19]);//6765
System.out.println ("----------------");

int a = 1;
int b = 1;
for (int x = 0; x < + × x + +) {
Temporary variable stores the last a
int temp = A;
A = b;
b = temp + b;
}
System.out.println (b);
System.out.println ("----------------");

System.out.println (FIB (20));
}

/*
* Method: Return value type: int parameter list: int n export condition: First month is 1, second month is 1 rule: starting from the third month, every one months is the first two months of the sum
*/
public static int fib (int n) {
if (n = = 1 | | n = = 2) {
return 1;
} else {
return fib (n-1) + fib (n-2);
}
}
}

Package cn.itcast_03;

Import Java.io.File;

/*
* Requirement: Please put the absolute path of all Java end files in the E:\JavaSE directory to the output in the console.
*
Analysis
* A: Package Directory
* B: Get file array for all files or folders in this directory
* C: Traverse the file array to get each file object
* D: Determine if the file object is a folder
* Yes: Back to B
* No: Continue to determine whether to end with. Java
* Yes: the absolute path of the file is output
* No: Ignore it
*/
public class Filepathdemo {
public static void Main (string[] args) {
Package Catalog
File Srcfolder = new file ("E:\\javase");

recursive function implementation
Getalljavafilepaths (Srcfolder);
}

private static void Getalljavafilepaths (File srcfolder) {
Get file array for all files or folders in this directory
file[] Filearray = Srcfolder.listfiles ();

Iterate over the file array to get each file object
for (File File:filearray) {
Determines whether the file object is a folder
if (File.isdirectory ()) {
Getalljavafilepaths (file);
} else {
Continue to determine whether to end with. Java
if (File.getname (). EndsWith (". Java")) {
The absolute path of the file is output
System.out.println (File.getabsolutepath ());
}
}
}
}
}


/*
* Requirements: Recursively delete directories with content
*
* Catalogue I have given: demo
*
Analysis
* A: Package Directory
* B: Get file array for all files or folders in this directory
* C: Traverse the file array to get each file object
* D: Determine if the file object is a folder
* Yes: Back to B
* No: Delete
*/
public class Filedeletedemo {
public static void Main (string[] args) {
Package Catalog
File Srcfolder = new File ("demo");
Recursive implementation
DeleteFolder (Srcfolder);
}

private static void DeleteFolder (File srcfolder) {
Get file array for all files or folders in this directory
file[] Filearray = Srcfolder.listfiles (); If this abstract pathname does not indicate that a directory or path does not exist, this method returns NULL. Otherwise, it returns an array of file objects, each of which corresponds to each file or directory in the directory, and if the folder does not have a file, the returned array object is not empty, only the array length is 0.

if (Filearray! = null) {//Because an enhanced for traversal object cannot be empty, you need to determine if the Filearray is empty
Iterate over the file array to get each file object
for (File File:filearray) {
Determines whether the file object is a folder
if (File.isdirectory ()) {
DeleteFolder (file);
} else {
System.out.println (File.getname () + "---" + file.delete ());//delete files
}
}

System.out.println (Srcfolder.getname () + "---" + srcfolder.delete ());//Because all files have been deleted, you can delete the parent class folder.
}
}
}

Recursive summary--day20

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.