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)