Description:
Loops are an integral part of learning programming. At the same time, recursion is inextricably related to the cycle.
example of a summation function:
//ask for 0 to N and. Sum failed returns-1. Otherwise, the result is returned. #include <iostream>//The most common loop notation. Long LongSum (unsigned int&& N) {Long Longsum =0; for(unsignedindex =1; Index <n+1; ++index) sum + = index;returnsum;}//recursive notationLong Long_sum (unsigned int&& N) {if(n = =0)return 0;return_sum (N-1) +n;}//The smartest notationLong Long__sum (unsigned int&& N) {Long Longsum = (1+ N) *n/2;returnsum;}intMain () {intA =Ten;STD::cout<< Sum (STD:: Move (a)) <<STD:: Endl;STD::cout<< Sum (Ten) <<STD:: Endl;STD::cout<< _sum (Ten) <<STD:: Endl;STD::cout<< __sum (Ten) <<STD:: Endl; System"Pause");return 0;}
Related explanations:
1. Use long long to avoid cross-border.
2. Use unsigned to avoid negative numbers.
3. Write with C++11 standard.
Sample Lookup functions:
#include <iostream>#include <assert.h>intFind_array (Const int*ConstArrConst unsigned& Length,Const int& value) {if(arr = =nullptr|| Length = =0)return-1; for(unsignedindex =0; Index < Length;++index)if(* (arr+index) = = value)returnIndexreturn-1;}//change to recursive test. int_find (unsignedIndexConst int*ConstArrConst unsigned& Length,Const int& value) {if(Arr[index] = = value)returnIndexif(arr = =nullptr|| Length = =0|| Index==length)return-1;return_find (Index +1, arr, length, value);}int_find_array (Const int*ConstArrConst unsigned& Length,Const int& value) {return_find (0, arr, length, value);}intMain () {intarr[Ten] = {1,2,3};STD::cout<< Find_array (arr,Ten,1) <<STD:: Endl;STD::cout<<_find_array (arr,Ten,Ten) <<STD:: Endl; System"Pause");return 0;}
Sample Print Container:
#include <iostream>#include <vector>voidPrint (Const STD:: vector<int>& Ivec,Const STD:: vector<int>::size_type& N) {if(Ivec.empty () | | n<=0)return; Print (Ivec, N-1);STD::cout<< ivec.at (N-1);}voidPrint_vector (Const STD:: vector<int>& Ivec) {Print (Ivec, Ivec.size ());}intMain () {STD:: vector<int>ivec{1,2,3,4}; Print_vector (Ivec);STD::cout<<STD:: Endl; System"Pause");return 0;}
example of a print chain:
#include <iostream>classnode{ Public:intData Node* Next; Node (int_data =0):d ATA (_data), Next (nullptr){}};usingPnode = Node *;//Print linked list:voidPrint_list (ConstNode* p) {if(p = =nullptr)return; while(p) {STD::cout<< P->data <<" "; p = p->next; }}//recursive notationvoidPrint (ConstPnode p) {if(p = =nullptr)return;STD::cout<< P->data <<" "; Print (P->next);}intMain () {Node P1, p2, p3; P1.data =1; P1.next = &p2; P2.data =2; P2.next = &p3; P3.data =3; P3.next =nullptr; Node *p = &p1; Print_list (P); Print (P);}
Summary:
1. The basic idea of recursion is to divide and conquer. In the second lookup function, it may be written that recursion is not so good, and recursive comprehension is more laborious. But it is acceptable to grasp the basic idea of division and administration. In the lookup function, the division is as follows: one by one, and this also belongs to the division of governance. But in the output function, divide and conquer is like this: output n-1 number and nth number, divide into two kinds. In short, to grasp deeply the connotation of division and governance, we must not simply think that recursion is just the function call itself.
2. Try to figure out the code and realize it yourself.
3. Refer to the blog: Poke me in to see the original
PS: The following should be recursive and loop-related content.
Loop and recursion of algorithm intersection (II.)