-
」ava Programming 一Recur引on- Waseda UnI監
-
Tqday's Topics g Understand-ng recursTve call of functlons o RecursEve functions . Seme examp[es ef the recursive cel]
-
Recursive call When some operations are cnlculntedfexecuted by e function the JAVA language e11ows the functien to ca]1 itseifwpthin the same program This Es called the recursTve call of a funct-on A recursive fuhctdon more recurslve cases has a trEviil result (w-thout (calls ]tself) and Advantages and disadvantages e [Advantage] Make thecodesimple " [Disadvantage] Difficult forimplementation e [D'is.advqnpage.] Perfarmeneqds.s.u.e.s. .emd rgqui're[srStack .spe.eg
-
Simple example of the recursive cali public class Sample.rec{ public static void mam(Stnng[] args) { recur$lon(e), System out prlnt("Xn"), } public static veid recursien(mt i> { lt{1<10){ recursion(i+1); System,out,prlnt(1+" "), } } } whWi's p'keg'ram pnih'ttt 987654BQIq on the screen
-
How to werk this program e " e e e e e First, this program calls the '`recurston'' function wvith the parameter O Since O is less than 10, the "recvrsFen" functien cells itselfwith the peremeteri+1(=1) ' Next, the ''recLirsoni' functton is also called wth the parameter 2 The ''recursion" functdon further ca[ls itse[f until the peremeter becemes rO When the pirameter ts 10, the "recurson'' functien returns from the recursbve call The "recursion" functien prints ''9'' on the screen .end returns This process repeits The pregrem ends when elj "recurslen" functiens returns from the recursive call
-
Example of the recursive Factortal e11=1 e21=tix2 o3i=2ix3・=(lrx2>x3 Fer general, rnt=(tt-1)1×v" =(e--2)1x(n-1)xv- t call 1 1/ ,C 1bl'r JLe ]pt e y[rtli" } 1tst4 ]./T :' L/r" 11 gyct M .Etu.,. I ].1 'YE t= El y..em tturn b 1 ] //1 g ' l y p;i 5ti]nglT 1 ), r L-/ U.//..T//!l/ n la o.e/ u' nt nzt/t Fnl /// .' nnfiT/1 , .d.. r!.1 n-l} , 1 c/1 /' uf= ' { s( 4(A ( 2(t!))D-l20
-
Example of the recursive call 2 Fibonaccisequence " f(o) -, o e fCD =1 g fe) - f(1)+j(o) -1 e f(3) = f(2) + f(1) = 2 "JCn)-f('rt-1)+l(r,-2) public static mt hbonacei(int ab f li(n == O){ return O, }el$e if(n =t 1){ return t, }elsla { return iib6nacc'i (h-t)+bbenEcci Cn-2) , tt } }
-
Exercise o thingstaken C, The C, isdefined q tnrl O,-1 -Irrt-L Ut ,i.Co e' Oditput of this ptogralth is as fellows' Inputn 20 Input b 3 2e q 3・ 1tsor
-
Answer lmport pstSllc ]ava util Scttnaer. classComblnatlon{ pubhc statlc vold maln(Strlng[] drgs) t 1nt n,r, System out pnnts("Inyput n 'b), n!]-putlotO, System owt pnnt("In?ut r "), r=lnputlntO, ] ptiblia static ]nt mputlntO { mt a= O. try{ Scanner sc ! ne- Scall-erCSYSted a = Bq nexOlnt O, , catclt {Exceptlen e] { e prlntStackTtaceO; System exrt{1), } return a, } ln), }
-
Answer IMFOri psbMc ]ata trtil Scanller ' clEss Combmatlon{ bubltc statSc voia mein(strs-g[] args) [ lnt ntrt System out prut(hlvput n "}, n=i-putlntO, SysLem out prl-t(''I"put r "}, r ! lnputIthtO, Systan ont printlllCn+"C"+r+"=" + combirtetlan(n,t)): , p"blic stetia lnt ztiputltstO { ITta=O, try{ Stan=er ee ! new SeannerCSyszem ln},; t = sc nextlntO. , gatch (lttreptitu e) { epr-nt9teckTlraceO, SystEm emt(1), } reeurn tr, 1 p"bl-e et.at.lc itrt vtimbitut.ionrint n,lnt rb { returncotnbiaationCn-tJr-i)+combi-afion(n-±,TL , 1
-
Answer ±MPOrt ja!" utl] Seantter, psbllc clasG Comb:=atlon{ pth1:e #ta"e vozd maln(Str:ngE] argn) { ±nt n,r, SJgtem out p=lntf''1oput n "V. 1 = ±nprttlttLO, Systen out prlntC"bprt r "), )!nnputlutO, System out,pnn:]nCn+"C"+r+"=" + cemblttatiemCn,=)), } publlc statlc lnt lnputlntO ( zttt a ! O, tTYC Scaaner st ! nev Sca"lerasYSteM IU}, a! sc nextlntO, } catch Cexeeptlon e) { eprlntSttickTracerl. Systgm elltCt), } =etprn a, } publze statlc lnt cemb!aatzeaClnt n,znt r') { if(r = O 11 r!!n]{ r-tu=p 1, ' lt(r L' t){ :etv=n n, ) rgtur]comb-nailo-Cn-t,r.t)+comblnat-o](m-#r)t lt }
-
Summary e Understanding recursive call of functions o Recurstve functiens o Some eyamples ef tHe tecursive call