C # Basic _ recursive methods several examples (13)

Source: Internet
Author: User

Recursion: It is itself that calls itself.

1  ///ask: To find the factorial of a number2         Static intFactor (intnum)3         {4             if(num==1)//decide when to start returning5             {6                 returnnum;7             }8             Else9             {Ten                 returnnum * Factor (--num);//the law of recursion One             } A}
1  //the product of all integers between two numbers (including boundaries)2         Static intGetResult (intNUM1,intnum2)3         {4             //First Judge Num1 and num2 which data is big5             if(Num1 >num2)6             {7                 inttemp =NUM1;8NUM1 =num2;9num2 =temp;Ten             } One             if(num2 ==num1)//determine under what circumstances the recursive call is no longer A             { -                 returnnum2; -             } the             Else -             { -                 returnNUM2 * GetResult (NUM1,--num2); -             } +}
1         //calculate array {1,1,2,3,5,8 ...} gets the number of the X position of the array index2         Static intGetindexof (intindex)3         {4 5             if(index==0|| index = =1)6             {7                 return 1;8             }9             ElseTen             { One                 returnGetindexof (Index-1) + getindexof (Index-2); A             } -}

C # Basic _ recursive methods several examples (13)

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.