Recursive algorithm of data structure and algorithm C + + and PHP implementations

Source: Internet
Author: User

Recursive algorithm: It is an algorithm that calls itself directly or indirectly. Implementation process: Through a function or a sub-process to complete, in a function or sub-process to write code directly or indirectly call themselves, you can complete the recursive operation. (The same category of problems, the problem layer into the scale of the sub-problem to the smallest problem hasknown conditions, and thento be solved, and then the results are returned step by step. is actually a kind of cycle. )The main embodiment:a small amount of code solves a very complex problemFeatures:1, recursion is the method calls itself2, must have a clear recursive end condition, called the recursive export. 3, concise but less efficient operation, generally do not promote the use of4, each layer of the return point, local variables, such as open up the stack to store, too many recursive times to cause stack overflow.
Example 1: Finding the factorial C + + code:
#include <iostream>int factorial (int n); int main () {    using namespace std;    int n;    cout << "Please enter a number:";    CIN >> N;    cout << n << "factorial is:" << factorial (n) <<endl;    return 0;} int factorial (int n) {    if (n = = 1)        return 1;    Return n*factorial (n-1);}
Operation Result:
Example 2: The numbering conversion
Code:
#include <iostream> #include <cstring>void feelthebase (char *s, int n, int sys), int main () {    using namespace Std;    Char s[60];    int N,sys;    cout << "Please enter an integer:";    CIN >> N;    cout << "Please enter the type of binary to be converted (2,8,16):";    CIN >> SYS;    Feelthebase (s, N, sys);    cout << n << "convert to" << sys << "binary result:" << s <<endl;    return 0;} void Feelthebase (char *s, int n, int sys) {    char bit[] = {"0123456789ABCDEF"};    int Len;    if (n = = 0)    {        strcpy (S, "");        return;    }    Feelthebase (S, N/sys, SYS);    Len = strlen (s);    S[len] = Bit[n%sys];    S[len+1] = ' + ';}
Operation Result:

Example 3:lists all subdirectories and files in a directory (you can also use theScandir function is more convenient)PHP Implementation code:
<?phpfunction Rec ($dir, $lev =0) {    $DH = Opendir ($dir), while (($file = Readdir ($DH))! = False) {  if ($file = =). ' || $file = = ' ... ') {   continue;  }  if (Is_dir ($dir. '/'. $file)) {      $arr = explode ("/", $dir. '/'. $file);   $lev = count ($arr)-3;      Echo Str_pad (", $lev,"--")." Catalog ". $file." <br/> ";   Rec ($dir. '/'. $file, $lev + 1);  } else {   echo str_pad (", $lev,"--"). $file." <br/> ";     } }  Closedir ($DH);} $dir = "./"; rec ($dir);? >
Operation Result:

Recursive algorithm of data structure and algorithm C + + and PHP implementations

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.