Chapter 12 - Recursion

Chapter 12 - Recursion - &KDSWHU 5HFXUVLRQ...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU 5HFXUVLRQ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 7KLQNLQJ 5HFXUVLYHO\ $ 1RQSURJUDPPLQJ ([DPSOH $Q ,WHUDWLYH 6ROXWLRQ &RQYHUWHG WR 5HFXUVLRQ 6WUXFWXUH RI 5HFXUVLYH 6ROXWLRQV $ %HWWHU ([DPSOH RI 5HFXUVLRQ 0HFKDQLFV RI 5HFXUVLRQ 5HFXUVLYH )XQFWLRQV ,QWHJHU ([SRQHQWLDWLRQ *UHDWHVW &RPPRQ 'LYLVRU 5HFXUVLYH *UDSKLFV RSWLRQDO &DVH 6WXG\ 3UHIL[ (YDOXDWRU ,QIL[ 3UHIL[ DQG 3RVWIL[ 1RWDWLRQ 3UHIL[ (YDOXDWRU &RPSOHWH 3URJUDP ,QWURGXFWLRQ 7KLV FKDSWHU IRFXVHV RQ D SURJUDPPLQJ WHFKQLTXH NQRZQ DV UHFXUVLRQ ,W DOORZV XV WR VROYH FHUWDLQ FRPSOH[ SUREOHPV LQ D KLJKO\ HOHJDQW PDQQHU 7KH FKDSWHU EHJLQV E\ FRPSDULQJ UHFXUVLRQ WR ZKDW ZH DOUHDG\ NQRZ 7KHQ LW GLVFXVVHV WKH ORZ OHYHO PHFKDQLFV WKDW PDNH UHFXUVLRQ ZRUN LQ -DYD )LQDOO\ ZH H[DPLQH D QXPEHU RI SUREOHPV WKDW DUH HDVLO\ H[SUHVVHG XVLQJ WKH WHFKQLTXH 609 7KLQNLQJ 5HFXUVLYHO\ 7KH SUREOHP VROYLQJ WHFKQLTXHV ZH KDYH VHHQ VR IDU DUH SDUW RI FODVVLFDO LWHUDWLRQ DOVR NQRZQ DV WKH LWHUDWLYH DSSURDFK ,WHUDWLRQ ,WHUDWLYH $ SURJUDPPLQJ WHFKQLTXH LQ ZKLFK \RX GHVFULEH DFWLRQV WR EH UHSHDWHG XVLQJ D ORRS ,Q WKLV FKDSWHU ZH ZLOO H[SORUH D QHZ WHFKQLTXH NQRZQ DV UHFXUVLRQ 5HFXUVLRQ 5HFXUVLYH $ SURJUDPPLQJ WHFKQLTXH LQ ZKLFK \RX GHVFULEH DFWLRQV WR EH UHSHDWHG XVLQJ D PHWKRG WKDW FDOOV LWVHOI <RX KDYH VSHQW VR PXFK WLPH ZULWLQJ VROXWLRQV LWHUDWLYHO\ WKDW LW ZLOO WDNH D ZKLOH WR JHW XVHG WR WKLQNLQJ DERXW SUREOHPV UHFXUVLYHO\ /HW V EHJLQ ZLWK D QRQSURJUDPPLQJ H[DPSOH $ 1RQSURJUDPPLQJ ([DPSOH :H RIWHQ ILQG RXUVHOYHV VWDQGLQJ LQ OLQH <RX PLJKW ILQG \RXUVHOI ZRQGHULQJ ZKDW SODFH \RX DUH LQ OLQH $UH \RX QXPEHU LQ OLQH" $UH \RX QXPEHU " +RZ ZRXOG \RX ILQG RXW" 0RVW SHRSOH ZRXOG VROYH WKLV SUREOHP LWHUDWLYHO\ <RX G FRXQW WKH SHRSOH LQ OLQH RQH WZR WKUHH DQG VR RQ 7KLV LV OLNH D ZKLOH ORRS WKDW FRQWLQXHV ZKLOH WKHUH DUH PRUH SHRSOH OHIW WR FRXQW 7KH LWHUDWLYH DSSURDFK LV D QDWXUDO RQH EXW LW KDV VRPH OLPLWDWLRQV )RU H[DPSOH ZKDW LI WKH SHUVRQ LQ IURQW RI \RX LV WDOO DQG \RX UH QRW YHU\ WDOO :LOO \RX EH DEOH WR VHH SDVW WKH SHUVRQ WR VHH H[DFWO\ ZKR LV LQ OLQH" 2U PD\EH WKH OLQH JRHV DURXQG WKH EORFN DQG \RX FDQ W VHH DURXQG WKH FRUQHU WR FRXQW WKH SHRSOH WKHUH &DQ \RX WKLQN RI DQRWKHU ZD\ WR VROYH WKH SUREOHP" 7R WKLQN DERXW WKH SUREOHP UHFXUVLYHO\ \RX KDYH WR WKLQN LQ WHUPV RI KDYLQJ DOO WKH SHRSOH VWDQGLQJ LQ OLQH ZRUNLQJ WRJHWKHU WR VROYH WKH SUREOHP 6R LQVWHDG RI KDYLQJ RQH SHUVRQ GRLQJ DOO RI WKH FRXQWLQJ \RX KDYH HDFK SHUVRQ GR D OLWWOH SLHFH RI WKH FRXQWLQJ 2QH DSSURDFK ZRXOG EH WR DVN WKH SHUVRQ LQ IURQW RI PH ZKDW P\ SODFH LQ OLQH LV 7KDW SHUVRQ PLJKW DVN DQRWKHU SHUVRQ ZKR PLJKW DVN DQRWKHU SHUVRQ %XW WKDW GRHVQ W KHOS PXFK EHFDXVH LW V WKH H[DFW VDPH TXHVWLRQ 7KDW MXVW OHDGV WR D EXQFK RI SHRSOH VD\LQJ 7KLV JX\ ZDQWV WR NQRZ ZKDW SODFH KH LV LQ OLQH 'RHV DQ\RQH NQRZ" 3UREDEO\ WKHQ VRPHRQH ZRXOG VWDUW FRXQWLQJ DQG VROYH WKH SUREOHP LWHUDWLYHO\ :H KDYH WR VRPHKRZ PDNH WKH SUREOHP VLPSOHU 6R LQVWHDG RI DVNLQJ WKH SHUVRQ LQ IURQW RI PH ZKDW SODFH , DP LQ OLQH , VKRXOG DVN WKH SHUVRQ ZKDW SODFH KH RU VKH LV LQ OLQH 610 +-----------------------------------------+ O -----| I want to know what place I am in line. | /|\ | So, what place are you in line? | | +-----------------------------------------+ / \ +--------------------------------+ O -----| Let me try to figure that out. | /|\ +--------------------------------+ | / \ 7KH NH\ GLIIHUHQFH LV WKDW WKH SHUVRQ LQ IURQW RI PH LV FORVHU WR WKH IURQW RI WKH OLQH 6XSSRVH IRU H[DPSOH WKDW , P WK LQ OLQH 7KHQ WKH SHUVRQ LQ IURQW RI PH LV WK LQ OLQH 7KDW V FORVHU WR WKH IURQW %XW QRWLFH WKDW , P DVNLQJ WKH SHUVRQ LQ IURQW RI PH WR WKLQN DERXW WKH H[DFW VDPH NLQG RI TXHVWLRQ , P WKLQNLQJ DERXW :H UH ERWK WU\LQJ WR ILJXUH RXW RXU SODFH LQ OLQH DOWKRXJK WKH SHUVRQ LQ IURQW RI PH LV FORVHU WR WKH IURQW WKDQ , DP 7KDW V ZKHUH WKH LGHD RI UHFXUVLRQ FRPHV LQ 7KH SUREOHP UHFXUV LQ WKDW HDFK RI XV ZDQWV WR DQVZHU WKH VDPH TXHVWLRQ 7KH LGHD LV WR VHW XS D FKDLQ UHDFWLRQ RI SHRSOH DOO DVNLQJ WKH SHUVRQ LQ IURQW RI WKHP WKH VDPH TXHVWLRQ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ ... 7KH OLQH KDV WR HQG VRPHZKHUH VR HYHQWXDOO\ WKLV SURFHVV KDV WR HQG (YHQWXDOO\ ZH KDYH WR UHDFK D SHUVRQ ZKR LV DW WKH IURQW RI WKH OLQH 611 ... +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +-------------------+ O -----| I'm at the front, | /|\ | so I'm 1st. | | +-------------------+ / \ $W WKLV SRLQW ZH KDYH UHDFKHG ZKDW ZH VRPHWLPHV UHIHU WR DV WKH ERWWRP RI WKH UHFXUVLRQ :H KDYH JRWWHQ D EXQFK RI SHRSOH LQYROYHG LQ FROOHFWLYHO\ VROYLQJ WKH SUREOHP DQG ZH YH ILQDOO\ UHDFKHG D SRLQW ZKHUH ZH FDQ VWDUW DVVHPEOLQJ WKH DQVZHU 7KH SHUVRQ DW WKH IURQW LV LQ SRVLWLRQ 7KDW PHDQV WKH SHUVRQ MXVW EHIRUH LV DW SRVLWLRQ DQG WKH SHUVRQ MXVW EHIRUH LV DW SRVLWLRQ DQG VR RQ 6R DIWHU ZH UHDFK WKH ERWWRP RI WKH UHFXUVLRQ ZH XQZLQG WKH UHFXUVLRQ DQG ILJXUH RXW WKH RYHUDOO DQVZHU +-------------------+ O -----| I'm at the front, | /|\ | so I'm 1st. | | +-------------------+ / \ +----------------+ O -----| Then I must be | /|\ | 2nd in line. | | +----------------+ / \ +----------------+ O -----| Then I must be | /|\ | 3rd in line. | | +----------------+ / \ +----------------+ O -----| Then I must be | /|\ | 4th in line. | | +----------------+ / \ 7KHVH GLDJUDPV LQFOXGHG MXVW IRXU LQGLYLGXDOV WR PDNH WKHP ILW RQ D SDJH EXW \RX FDQ LPDJLQH WKLV SURFHVV ZRUNLQJ HYHQ LI WKHUH ZHUH SHRSOH LQ OLQH RU SHRSOH LQ OLQH 2QH RI WKH NH\ DVSHFWV WR QRWLFH KHUH LV WKDW UHFXUVLRQ LQYROYHV PDQ\ FRRSHUDWLQJ HQWLWLHV WKDW HDFK VROYH D OLWWOH ELW RI WKH SUREOHP ,QVWHDG RI RQH SHUVRQ ZKR GRHV DOO RI WKH FRXQWLQJ ZH KDYH HDFK LQGLYLGXDO DVNLQJ RQH TXHVWLRQ DV ZH JR WRZDUGV WKH IURQW RI WKH OLQH DQG DQVZHULQJ RQH TXHVWLRQ DV ZH FRPH EDFN RXW 612 ,Q SURJUDPPLQJ WKH LWHUDWLYH VROXWLRQ RI KDYLQJ RQH SHUVRQ GRLQJ DOO WKH FRXQWLQJ LV OLNH KDYLQJ D ORRS WKDW UHSHDWV VRPH DFWLRQ 7KH UHFXUVLYH VROXWLRQ RI KDYLQJ PDQ\ SHRSOH HDFK GRLQJ D OLWWOH ELW RI ZRUN ZLOO WXUQ LQWR PDQ\ GLIIHUHQW PHWKRG FDOOV HDFK RI ZKLFK SHUIRUP D OLWWOH ELW RI ZRUN 6R OHW V VHH DQ H[DPSOH RI KRZ D VLPSOH LWHUDWLYH VROXWLRQ FRXOG EH WXUQHG LQWR D UHFXUVLYH VROXWLRQ $Q ,WHUDWLYH 6ROXWLRQ &RQYHUWHG WR 5HFXUVLRQ 6XSSRVH WKDW \RX ZDQW WR KDYH D PHWKRG FDOOHG ZULWH6WDUV WKDW ZLOO WDNH DQ LQWHJHU SDUDPHWHU Q DQG WKDW ZLOO SURGXFH D OLQH RI RXWSXW ZLWK H[DFWO\ Q VWDUV RQ LW :H FDQ VROYH WKLV ZLWK D VLPSOH IRU ORRS public static void writeStars(int n) { for (int i = 1; i <= n; i++) { System.out.print("*"); } System.out.println(); } 7KH DFWLRQ EHLQJ UHSHDWHG KHUH LV WKH FDOO RQ 6\VWHP RXW SULQW WKDW SULQWV WKH Q VWDUV ,I ZH ZDQW WR ZULWH WKLV UHFXUVLYHO\ ZH QHHG WR WKLQN DERXW GLIIHUHQW FDVHV :H PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH ZLWK VWDUV RU ZH PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH ZLWK VWDUV RU ZH PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH RI VWDUV 2I DOO RI WKH SRVVLEOH VWDU ZULWLQJ WDVNV ZH PLJKW DVN LW WR SHUIRUP ZKLFK RQH LV WKH VLPSOHVW" 6WXGHQWV RIWHQ DQVZHU WKDW SULQWLQJ D OLQH RI VWDU LV YHU\ HDV\ DQG WKH\ UH ULJKW WKDW LW V HDV\ %XW WKHUH LV D WDVN WKDW LV HYHQ HDVLHU 3ULQWLQJ D OLQH RI VWDUV UHTXLUHV DOPRVW QR ZRUN DW DOO :H FDQ FUHDWH VXFK D OLQH E\ FDOOLQJ 6\VWHP RXW SULQWOQ 6R ZH EHJLQ RXU UHFXUVLYH GHILQLWLRQ ZLWK D WHVW IRU WKLV FDVH public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { ... } ,Q WKH HOVH SDUW ZH ZDQW WR GHDO ZLWK OLQHV WKDW KDYH PRUH WKDQ VWDUV RQ WKHP <RXU LQVWLQFW LV SUREDEO\ WR ILOO LQ WKH HOVH SDUW ZLWK WKH IRU ORRS ZH XVHG EHIRUH <RX KDYH WR ILJKW WKH LQVWLQFW WR VROYH WKH HQWLUH SUREOHP WKDW ZD\ 7R VROYH WKLV VHFRQG SDUW RI WKH SUREOHP LW LV LPSRUWDQW WR WKLQN DERXW KRZ ZH FDQ GR MXVW D VPDOO DPRXQW RI ZRUN WKDW ZLOO JHW XV FORVHU WR EHLQJ GRQH ,I WKH QXPEHU RI VWDUV LV JUHDWHU WKDQ WKHQ ZH NQRZ ZH KDYH WR SULQW DW OHDVW RQH VR ZH FDQ DGG WKDW WR RXU FRGH public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { System.out.print("*"); // what is left to do? ... } 613 $W WKLV SRLQW LQ WKH SURFHVV \RX KDYH WR EH DEOH WR PDNH D FHUWDLQ OHDS RI IDLWK <RX KDYH WR EHOLHYH WKDW UHFXUVLRQ DFWXDOO\ ZRUNV 2QFH ZH KDYH ZULWWHQ D VLQJOH VWDU ZKDW GR ZH KDYH OHIW WR GR" 7KH DQVZHU LV WKDW ZH ZDQW WR ZULWH Q PRUH VWDUV DORQJ ZLWK D SULQWOQ ,Q RWKHU ZRUGV DIWHU ZULWLQJ RQH VWDU ZKDW ZH KDYH OHIW WR GR LV WR ZULWH D OLQH RI Q VWDUV <RX VKRXOG ILQG \RXUVHOI WKLQNLQJ ,I RQO\ , KDG D PHWKRG WKDW ZRXOG SURGXFH D OLQH RI Q VWDUV WKHQ , FRXOG FDOO WKDW PHWKRG %XW \RX GR KDYH VXFK D PHWKRG ,W V WKH PHWKRG ZH DUH ZULWLQJ 6R DIWHU ZULWLQJ D VLQJOH VWDU ZH FDQ FDOO WKH ZULWH6WDUV PHWKRG LWVHOI WR FRPSOHWH WKH OLQH RI RXWSXW public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { System.out.print("*"); writeStars2(n - 1); } } 0DQ\ QRYLFHV FRPSODLQ WKDW WKLV VHHPV OLNH FKHDWLQJ :H DUH VXSSRVHG WR EH ZULWLQJ WKH PHWKRG FDOOHG ZULWH6WDUV VR KRZ FDQ ZH FDOO ZULWH6WDUV IURP LQVLGH RI ZULWH6WDUV " :HOFRPH WR WKH ZRUOG RI UHFXUVLRQ ,Q RXU QRQSURJUDPPLQJ H[DPSOH ZH WDONHG DERXW KDYLQJ PDQ\ SHRSOH VWDQGLQJ LQ OLQH ZKR VROYHG D SUREOHP WRJHWKHU 7R XQGHUVWDQG D UHFXUVLYH PHWKRG OLNH ZULWH6WDUV LW LV XVHIXO WR LPDJLQH HDFK PHWKRG LQYRFDWLRQ DV EHLQJ OLNH D SHUVRQ 7KH NH\ LQVLJKW LV WKDW WKHUH LVQ W MXVW RQH SHUVRQ ZKR FDQ GR WKH ZULWH6WDUV WDVN :H KDYH DQ HQWLUH DUP\ RI SHRSOH ZKR FDQ HDFK GR WKH ZULWH6WDUV WDVN /HW V WKLQN DERXW ZKDW KDSSHQV ZKHQ , FDOO WKH PHWKRG DQG UHTXHVW D OLQH RI writeStars2(3); VWDUV :H FDOO XS WKH ILUVW SHUVRQ IURP WKH ZULWH6WDUV DUP\ DQG VD\ , ZDQW D OLQH RI VWDUV 7KH SHUVRQ ORRNV DW WKH FRGH LQ WKH PHWKRG DQG VHHV WKDW WKH ZD\ \RX ZULWH D OLQH RI VWDUV LV WR System.out.print("*"); writeStars2(2); ,Q RWKHU ZRUGV WKH ILUVW PHPEHU RI WKH DUP\ ZULWHV D VWDU DQG FDOOV XS WKH QH[W PHPEHU RI WKH DUP\ WR ZULWH D OLQH RI VWDUV -XVW DV ZH KDG D VHULHV RI SHRSOH ILJXULQJ RXW ZKDW SODFH WKH\ ZHUH LQ OLQH ZH KDYH D VHULHV RI SHRSOH FDOOHG XS ZKR HDFK SULQW RQH VWDU DQG WKHQ FDOO RQ VRPHRQH HOVH WR ZULWH WKH UHVW RI WKH OLQH :LWK WKH SHRSOH VWDQGLQJ LQ OLQH ZH HYHQWXDOO\ UHDFKHG WKH SHUVRQ DW WKH IURQW RI WKH OLQH ,Q WKLV FDVH ZH UHDFK D SRLQW ZKHUH WKH UHTXHVW LV WR ZULWH D OLQH RI VWDUV ZKLFK OHDGV XV LQWR WKH LI EUDQFK UDWKHU WKDQ WKH HOVH EUDQFK DQG ZH FRPSOHWH WKH WDVN ZLWK D VLPSOH SULQWOQ %HORZ LV D WUDFH RI WKH FDOOV WKDW ZRXOG EH PDGH writeStars2(3); // n > 0, execute else System.out.print("*"); writeStars2(2); // n > 0, execute else System.out.print("*"); writeStars2(1); // n > 0, execute else System.out.print("*"); writeStars2(0); // n == 0, execute if System.out.println(); 614 6R D WRWDO RI IRXU GLIIHUHQW FDOOV DUH PDGH RQ WKH PHWKRG ,I ZH WKLQN RI WKH FDOOV DV EHLQJ OLNH SHRSOH D WRWDO RI IRXU PHPEHUV RI WKH DUP\ DUH FDOOHG XS WR VROYH WKH WDVN WRJHWKHU (DFK RQH ZDV VROYLQJ D VWDU ZULWLQJ WDVN EXW WKH\ ZHUH VROYLQJ VOLJKWO\ GLIIHUHQW WDVNV VWDUV VWDUV VWDU VWDUV 7KLV LV VLPLODU WR WKH QRQSURJUDPPLQJ H[DPSOH LQ ZKLFK WKH YDULRXV SHRSOH VWDQGLQJ LQ OLQH ZHUH DOO DQVZHULQJ WKH VDPH NLQG RI TXHVWLRQ EXW WKH\ ZHUH VROYLQJ VOLJKWO\ GLIIHUHQW SUREOHPV EHFDXVH RI ZKHUH WKH\ ZHUH VWDQGLQJ LQ OLQH FORVHU WR WKH IURQW RU IXUWKHU IURP WKH IURQW 6WUXFWXUH RI 5HFXUVLYH 6ROXWLRQV :ULWLQJ UHFXUVLYH VROXWLRQV UHTXLUHV D FHUWDLQ OHDS RI IDLWK EXW WKHUH LV QRWKLQJ PDJLFDO DERXW UHFXUVLRQ )RU H[DPSOH WKH IROORZLQJ PHWKRG LV QRW D VROXWLRQ WR WKH WDVN RI ZULWLQJ D OLQH RI Q VWDUV public static void writeStars3(int n) { writeStars3(n); } 7KLV YHUVLRQ SURGXFHV LQILQLWH UHFXUVLRQ )RU H[DPSOH LI , DVN WKH PHWKRG WR ZULWH D OLQH RI VWDUV LW WULHV WR DFFRPSOLVK WKDW E\ DVNLQJ WKH PHWKRG WR ZULWH D OLQH RI VWDUV ZKLFK DVNV WKH PHWKRG WR ZULWH D OLQH RI VWDUV ZKLFK DVNV WKH PHWKRG WR ZULWH D OLQH RI VWDUV DQG VR RQ ,Q RWKHU ZRUGV WKLV VROXWLRQ LV WKH UHFXUVLYH HTXLYDOHQW RI DQ LQILQLWH ORRS (YHU\ UHFXUVLYH VROXWLRQ WKDW \RX ZULWH ZLOO KDYH WZR NH\ LQJUHGLHQWV D EDVH FDVH DQG D UHFXUVLYH FDVH %DVH &DVH $ FDVH WKDW LV VR VLPSOH WKDW LW FDQ EH VROYHG GLUHFWO\ ZLWKRXW D UHFXUVLYH FDOO 5HFXUVLYH &DVH $ FDVH WKDW LQYROYHV UHGXFLQJ WKH RYHUDOO SUREOHP WR D VLPSOHU SUREOHP RI WKH VDPH NLQG WKDW FDQ EH VROYHG E\ D UHFXUVLYH FDOO %HORZ LV WKH ZULWH6WDUV PHWKRG ZLWK LWV EDVH FDVH DQG UHFXUVLYH FDVH LQGLFDWHG ZLWK FRPPHQWV public static void writeStars2(int n) { if (n == 0) { // base case System.out.println(); } else { // recursive case System.out.print("*"); writeStars2(n - 1); } } 615 7KH EDVH FDVH LV WKH WDVN RI ZULWLQJ D OLQH RI ]HUR VWDUV 7KLV WDVN LV VR VLPSOH WKDW LW FDQ EH GRQH LPPHGLDWHO\ 7KH UHFXUVLYH FDVH LV WKH WDVN RI ZULWLQJ OLQHV ZLWK RU PRUH VWDUV 7R VROYH WKH UHFXUVLYH FDVH ZH EHJLQ E\ ZULWLQJ D VLQJOH VWDU ZKLFK UHGXFHV WKH UHPDLQLQJ WDVN WR WKDW RI ZULWLQJ D OLQH RI Q VWDUV 7KLV LV D WDVN WKDW WKH ZULWH6WDUV PHWKRG LV GHVLJQHG WR VROYH DQG LW LV VLPSOHU WKDQ WKH RULJLQDO WDVN VR ZH FDQ VROYH LW E\ PDNLQJ D UHFXUVLYH FDOO $V DQ DQDORJ\ VXSSRVH WKDW \RX DUH DW WKH WRS RI D ODGGHU ZLWK Q UXQJV RQ LW ,I \RX KDYH D ZD\ WR JHW IURP RQH UXQJ WR WKH RQH EHORZ DQG LI \RX FDQ UHFRJQL]H ZKHQ \RX YH UHDFKHG WKH JURXQG WKHQ \RX FDQ KDQGOH DQ\ KHLJKW ODGGHU *RLQJ IURP RQH UXQJ WR WKH RQH EHORZ LV OLNH WKH UHFXUVLYH FDVH ZKHUH \RX SHUIRUP VRPH VPDOO DPRXQW RI ZRUN WKDW UHGXFHV WKH SUREOHP WR D VLPSOHU RQH RI WKH VDPH IRUP JHW GRZQ IURP UXQJ Q YHUVXV JHW GRZQ IURP UXQJ Q 5HFRJQL]LQJ ZKHQ \RX UHDFK WKH JURXQG LV OLNH WKH EDVH FDVH WKDW FDQ EH VROYHG GLUHFWO\ VWHS RII WKH ODGGHU 6RPH SUREOHPV LQYROYH PXOWLSOH EDVH FDVHV DQG VRPH SUREOHPV LQYROYH PXOWLSOH UHFXUVLYH FDVHV EXW WKHUH ZLOO DOZD\V EH DW OHDVW RQH RI HDFK ,I \RX DUH PLVVLQJ HLWKHU \RX UXQ LQWR WURXEOH :LWKRXW WKH DELOLW\ WR VWHS GRZQ IURP RQH UXQJ WR WKH RQH EHORZ \RX G EH VWXFN DW WKH WRS RI WKH ODGGHU :LWKRXW WKH DELOLW\ WR UHFRJQL]H ZKHQ \RX UHDFK WKH JURXQG \RX G NHHS FOLPELQJ HYHQ ZKHQ WKHUH DUH QR UXQJV OHIW LQ WKH ODGGHU %HFDXVH UHFXUVLYH VROXWLRQV LQFOXGH VRPH FRPELQDWLRQ RI EDVH FDVHV DQG UHFXUVLYH FDVHV \RX ZLOO ILQG WKDW WKH\ DUH RIWHQ ZULWWHQ ZLWK DQ LI HOVH VWDWHPHQW D QHVWHG LI VWDWHPHQW RU VRPH PLQRU YDULDWLRQ <RX ZLOO DOVR ILQG WKDW UHFXUVLYH SURJUDPPLQJ JHQHUDOO\ LQYROYHV D FDVH DQDO\VLV ZKHUH \RX FDWHJRUL]H WKH SRVVLEOH IRUPV WKH SUREOHP PLJKW WDNH LQWR GLIIHUHQW FDVHV DQG \RX ZULWH D VROXWLRQ IRU HDFK FDVH $ %HWWHU ([DPSOH RI 5HFXUVLRQ :KLOH LW PLJKW EH LQWHUHVWLQJ WR VROYH WKH ZULWH6WDUV WDVN ZLWK UHFXUVLRQ LW LVQ W D YHU\ FRPSHOOLQJ H[DPSOH /HW V ORRN LQ GHWDLO DW D SUREOHP ZKHUH UHFXUVLRQ VLPSOLILHV WKH ZRUN ZH KDYH WR GR 6XSSRVH WKDW \RX KDYH D 6FDQQHU WKDW LV WLHG WR DQ H[WHUQDO LQSXW ILOH DQG \RX ZDQW WR SULQW WKH OLQHV RI WKH ILOH LQ UHYHUVH RUGHU )RU H[DPSOH WKH ILOH PLJKW FRQWDLQ WKH IROORZLQJ IRXU OLQHV RI WH[W this is fun no? :H ZDQW WR SULQW WKHVH OLQHV LQ UHYHUVH RUGHU VR ZH G OLNH WR SURGXFH WKLV RXWSXW no? fun is this 7R VROYH WKH SUREOHP LWHUDWLYHO\ ZH G QHHG VRPH NLQG RI GDWD VWUXFWXUH IRU VWRULQJ WKH OLQHV RI WH[W :H PLJKW IRU H[DPSOH XVH DQ $UUD\/LVW6WULQJ! :LWK UHFXUVLRQ ZH FDQ VROYH WKH SUREOHP ZLWKRXW XVLQJ D GDWD VWUXFWXUH 616 5HPHPEHU WKDW UHFXUVLYH SURJUDPPLQJ LQYROYHV WKLQNLQJ DERXW FDVHV :KDW ZRXOG EH WKH VLPSOHVW ILOH WR UHYHUVH" $ RQH OLQH ILOH ZRXOG EH IDLUO\ HDV\ WR UHYHUVH EXW LW ZRXOG EH HYHQ HDVLHU WR UHYHUVH DQ HPSW\ ILOH 6R ZH FDQ EHJLQ ZULWLQJ RXU PHWKRG DV IROORZV public static void reverse(Scanner input) { if (!input.hasNextLine()) { // base case (empty file) ... } else { // recursive case (nonempty file) ... } } )RU WKLV SUREOHP WKH EDVH FDVH LV VR VLPSOH WKDW WKHUH LVQ W DQ\WKLQJ WR GR $Q HPSW\ ILOH KDV QR OLQHV WR UHYHUVH 6R IRU WKLV SUREOHP LW PDNHV PRUH VHQVH WR WXUQ DURXQG WKH LI HOVH VR WKDW ZH WHVW IRU WKH UHFXUVLYH FDVH 7KDW ZD\ ZH FDQ PDNH LW D VLPSOH LI WKDW KDV DQ LPSOLHG HOVH WKHUH LV QRWKLQJ WR GR public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) ... } } $JDLQ WKH FKDOOHQJH LV WR GR D OLWWOH ELW +RZ GR ZH JHW D ELW FORVHU WR EHLQJ GRQH" :H FDQ UHDG RQH OLQH RI WH[W IURP WKH ILOH public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) String line = input.nextLine(); ... } } 6R LQ RXU H[DPSOH WKLV ZRXOG UHDG WKH OLQH WKLV LQWR WKH YDULDEOH OLQH DQG ZRXOG OHDYH XV ZLWK WKHVH WKUHH OLQHV RI WH[W LQ WKH 6FDQQHU is fun no? :H UH WU\LQJ WR SURGXFH WKLV RYHUDOO RXWSXW no? fun is this <RX PLJKW EH DVNLQJ \RXUVHOI TXHVWLRQV OLNH ,V WKHUH DQRWKHU OLQH RI LQSXW WR SURFHVV" 7KDW V QRW KRZ WR WKLQN UHFXUVLYHO\ ,I \RX UH WKLQNLQJ UHFXUVLYHO\ \RX G WKLQN DERXW ZKDW D FDOO RQ WKH PHWKRG ZRXOG JHW \RX 6LQFH WKH 6FDQQHU LV SRVLWLRQHG LQ IURQW RI WKH WKUHH OLQHV LV IXQ QR" D FDOO RQ UHYHUVH VKRXOG UHDG LQ WKRVH OLQHV DQG SURGXFH WKH ILUVW WKUHH OLQHV RI RXWSXW WKDW ZH UH ORRNLQJ IRU ,I WKDW ZRUNV WKHQ ZH G MXVW KDYH WR ZULWH RXW WKH OLQH WKLV DIWHUZRUGV WR FRPSOHWH WKH RXWSXW 617 7KLV LV ZKHUH WKH OHDS RI IDLWK FRPHV LQ :H KDYH WR EHOLHYH WKDW WKH UHYHUVH PHWKRG DFWXDOO\ ZRUNV %HFDXVH LI LW GRHV WKHQ WKLV FRGH FDQ EH FRPSOHWHG DV IROORZV public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) String line = input.nextLine(); reverse(input); System.out.println(line); } } ,W WXUQV RXW WKDW WKLV ZRUNV 7R UHYHUVH D VHTXHQFH RI OLQHV \RX UHDG WKH ILUVW RQH LQ UHYHUVH WKH RWKHUV DQG WKHQ ZULWH WKH ILUVW RQH RXW ,W GRHVQ W VHHP WKDW LW VKRXOG EH WKDW VLPSOH EXW LW LV 0HFKDQLFV RI 5HFXUVLRQ 1RYLFHV VHHP WR XQGHUVWDQG UHFXUVLRQ EHWWHU ZKHQ WKH\ NQRZ PRUH DERXW WKH XQGHUO\LQJ PHFKDQLFV WKDW PDNH LW ZRUN %HIRUH ZH H[DPLQH D UHFXUVLYH PHWKRG LQ GHWDLO ZH VKRXOG PDNH VXUH ZH XQGHUVWDQG KRZ QRQUHFXUVLYH PHWKRGV ZRUN &RQVLGHU WKH IROORZLQJ VLPSOH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class DrawTriangles { public static void drawTriangle() { System.out.println(" *"); System.out.println(" ***"); System.out.println("*****"); System.out.println(); } public static void drawTwoTriangles() { drawTriangle(); drawTriangle(); } } ,W VLPSO\ SULQWV WKUHH WULDQJOHV * *** ***** * *** ***** * *** ***** +RZ GR ZH GHVFULEH WKH PHWKRG FDOOV WKDW WDNH SODFH LQ WKLV SURJUDP" ,PDJLQH WKDW HDFK PHWKRG KDV EHHQ ZULWWHQ RQ D GLIIHUHQW SLHFH RI SDSHU :H EHJLQ SURJUDP H[HFXWLRQ E\ JRLQJ WR PHWKRG PDLQ VR LPDJLQH JUDEELQJ WKH VKHHW RI SDSHU ZLWK PDLQ RQ LW 618 +---------------------------------------------+ | public static void main(String args) { | | drawTriangle(); | | drawTwoTriangles(); | | } | +---------------------------------------------+ 7R H[HFXWH WKH PHWKRG ZH JR WKURXJK HDFK RI WKH VWDWHPHQWV IURP ILUVW WR ODVW DQG H[HFXWH HDFK RQH 6R ZH ILUVW KLW WKH FDOO RQ GUDZ7ULDQJOH +---------------------------------------------+ | public static void main(String args) { | | --> drawTriangle(); | | drawTwoTriangles(); | | } | +---------------------------------------------+ :H NQRZ WKDW DW WKLV SRLQW LQ WLPH WKH FRPSXWHU VWRSV H[HFXWLQJ PDLQ DQG WXUQV LWV DWWHQWLRQ WR WKH GUDZ7ULDQJOH PHWKRG $ JRRG ZD\ WR WKLQN DERXW WKLV LV WKDW \RX JUDE WKH SLHFH RI SDSHU ZLWK GUDZ7ULDQJOH ZULWWHQ RQ LW DQG \RX SXW LW RYHU WKH SLHFH RI SDSHU ZLWK PDLQ RQ LW +----------------------------------------------+ | public static void main(String args) { | | +----------------------------------------------+ | | public static void drawTriangle() { | | | System.out.println(" *"); | +--| System.out.println(" ***"); | | System.out.println("*****"); | | System.out.println(); | | } | +----------------------------------------------+ 1RZ ZH H[HFXWH HDFK RI WKH VWDWHPHQWV LQ GUDZ7ULDQJOH IURP ILUVW WR ODVW DQG JR EDFN WR PDLQ :KHQ ZH JR EDFN WR PDLQ ZH ZLOO KDYH ILQLVKHG WKH FDOO RQ GUDZ7ULDQJOH ZKLFK PHDQV WKDW WKH QH[W WKLQJ WR GR LV WKH FDOO RQ GUDZ7ZR7ULDQJOHV +---------------------------------------------+ | public static void main(String args) { | | drawTriangle(); | | --> drawTwoTriangles(); | | } | +---------------------------------------------+ 6R QRZ ZH JUDE WKH SLHFH RI SDSHU ZLWK GUDZ7ZR7ULDQJOHV RQ LW DQG ZH SODFH LW RYHU WKH SDSHU ZLWK PDLQ RQ LW +----------------------------------------------+ | public static void main(String args) { | | +----------------------------------------------+ | | public static void drawTwoTriangles() { | | | drawTriangle(); | +--| drawTriangle(); | | } | +----------------------------------------------+ 7KH ILUVW WKLQJ WR GR KHUH LV WKH ILUVW FDOO RQ GUDZ7ULDQJOH 619 +----------------------------------------------+ | public static void main(String args) { | | +----------------------------------------------+ | | public static void drawTwoTriangles() { | | | --> drawTriangle(); | +--| drawTriangle(); | | } | +----------------------------------------------+ 7R H[HFXWH WKLV PHWKRG ZH WDNH RXW WKH VKHHW RI SDSHU ZLWK GUDZ7ULDQJOH RQ LW DQG SODFH LW RQ WRS +----------------------------------------------+ | public static void main(String args) { | | +----------------------------------------------+ | | public static void drawTwoTriangles() { | | | +----------------------------------------------+ | | | public static void drawTriangle() { | | | | System.out.println(" *"); | +--| | System.out.println(" ***"); | | | System.out.println("*****"); | +--| System.out.println(); | | } | +----------------------------------------------+ %\ SXWWLQJ WKHVH SDSHUV RQH RQ WRS RI WKH RWKHU ZH FDQ YLVXDOO\ VHH WKDW ZH VWDUWHG ZLWK PHWKRG PDLQ ZKLFK FDOOHG PHWKRG GUDZ7ZR7ULDQJOHV ZKLFK FDOOHG PHWKRG GUDZ7ULDQJOH 6R DW WKLV PRPHQW LQ WLPH WKUHH GLIIHUHQW PHWKRGV DUH DFWLYH 7KH RQH RQ WRS LV WKH RQH WKDW ZH DUH DFWLYHO\ H[HFXWLQJ 2QFH ZH ILQLVK ZLWK LW ZH JR EDFN WR WKH RQH XQGHUQHDWK $QG RQFH ZH ILQLVK LW ZH OO JR EDFN WR PDLQ :H FRXOG FRQWLQXH WKLV H[DPSOH EXW \RX SUREDEO\ JHW WKH LGHD E\ QRZ 7KH LGHD RI UHSUHVHQWLQJ HDFK PHWKRG FDOO DV D SLHFH RI SDSHU DQG SXWWLQJ WKHP RQ WRS RI HDFK RWKHU DV WKH\ DUH FDOOHG LV D PHWDSKRU IRU -DYD V FDOO VWDFN &DOO 6WDFN 7KH LQWHUQDO VWUXFWXUH WKDW NHHSV WUDFN RI WKH VHTXHQFH RI PHWKRGV WKDW KDYH EHHQ FDOOHG ,I \RX WKLQN RI WKH FDOO VWDFN DV EHLQJ OLNH D VWDFN RI SDSHUV ZLWK WKH PRVW UHFHQWO\ FDOOHG PHWKRG RQ WRS \RX KDYH D SUHWW\ JRRG LGHD RI KRZ LW ZRUNV /HW V XVH WKH LGHD RI WKH FDOO VWDFN WR XQGHUVWDQG KRZ WKH UHFXUVLYH ILOH UHYHUVLQJ PHWKRG ZRUNV 7R EH DEOH WR YLVXDOL]H WKH FDOO VWDFN ZH QHHG WR SXW WKH PHWKRG GHILQLWLRQ RQ D SLHFH RI SDSHU 620 +-----------------------------------------------+ | public static void reverse(Scanner input) { | | if (input.hasNextLine()) { | | String line = input.nextLine(); | | reverse(input); | | System.out.println(line); | | } | | } | | +------------------+ | | line | | | | +------------------+ | +-----------------------------------------------+ 1RWLFH WKDW WKH SDSHU LQFOXGHV D SODFH WR VWRUH WKH YDOXH RI WKH ORFDO YDULDEOH FDOOHG OLQH 7KLV GHWDLO ZLOO WXUQ RXW WR EH YHU\ LPSRUWDQW 6XSSRVH WKDW ZH FDOO WKLV PHWKRG ZLWK RXU VDPSOH LQSXW ILOH ZLWK WKH IROORZLQJ IRXU OLQHV RI WH[W this is fun no? :KHQ ZH FDOO WKH PHWKRG LW UHDGV WKH ILUVW OLQH RI WH[W LQWR LWV YDULDEOH OLQH DQG WKHQ LW UHDFKHV WKH UHFXUVLYH FDOO RQ UHYHUVH +-----------------------------------------------+ | public static void reverse(Scanner input) { | | if (input.hasNextLine()) { | | String line = input.nextLine(); | | --> reverse(input); | | System.out.println(line); | | } | | } | | +------------------+ | | line | "this" | | | +------------------+ | +-----------------------------------------------+ 7KHQ ZKDW KDSSHQV" ,Q WKH WULDQJOH SURJUDP ZH JUDEEHG WKH VKHHW RI SDSHU IRU WKH PHWKRG EHLQJ FDOOHG DQG SODFHG LW RQ WRS RI WKH FXUUHQW VKHHW RI SDSHU +HUH ZH KDYH PHWKRG UHYHUVH FDOOLQJ PHWKRG UHYHUVH 7R XQGHUVWDQG ZKDW KDSSHQV \RX KDYH WR UHDOL]H WKDW HDFK PHWKRG LQYRFDWLRQ LV LQGHSHQGHQW RI WKH RWKHUV :H GRQ W KDYH MXVW D VLQJOH VKHHW RI SDSHU ZLWK WKH UHYHUVH PHWKRG ZULWWHQ RQ LW ZH KDYH DV PDQ\ FRSLHV DV ZH ZDQW 6R ZH FDQ JUDE D VHFRQG FRS\ RI WKH PHWKRG GHILQLWLRQ DQG SODFH LW RQ WRS RI WKLV RQH 621 +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | if (input.hasNextLine()) { | | | String line = input.nextLine(); | | | reverse(input); | | | System.out.println(line); | | | } | | | } | | | +------------------+ | +--| line | | | | +------------------+ | +-----------------------------------------------+ 7KLV QHZ YHUVLRQ RI WKH PHWKRG KDV LWV RZQ YDULDEOH FDOOHG OLQH LQ ZKLFK LW FDQ VWRUH D OLQH RI WH[W $QG HYHQ WKRXJK WKH SUHYLRXV YHUVLRQ WKH RQH XQGHUQHDWK WKLV RQH LV LQ WKH PLGGOH RI LWV H[HFXWLRQ WKLV QHZ RQH LV DW WKH EHJLQQLQJ RI LWV H[HFXWLRQ 7KLQN EDFN WR WKH DQDORJ\ RI DQ HQWLUH DUP\ RI URERWV DOO SURJUDPPHG ZLWK WKH VDPH FRGH ,Q WKH VDPH ZD\ ZH FDQ EULQJ XS DV PDQ\ FRSLHV RI WKH UHYHUVH PHWKRG DV ZH QHHG WR VROYH WKLV SUREOHP )RU WKLV VHFRQG FDOO RQ UHYHUVH ZH VHH WKDW WKHUH LV D OLQH RI WH[W WR EH UHDG DQG LW UHDGV LW LQ WKH VHFRQG OLQH WKDW FRQWDLQV LV 7KHQ LW ILQGV LWVHOI H[HFXWLQJ D UHFXUVLYH FDOO RQ UHYHUVH +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | if (input.hasNextLine()) { | | | String line = input.nextLine(); | | | --> reverse(input); | | | System.out.println(line); | | | } | | | } | | | +------------------+ | +--| line | "is" | | | +------------------+ | +-----------------------------------------------+ 6R -DYD VHWV DVLGH WKLV YHUVLRQ RI WKH PHWKRG DV ZHOO DQG EULQJV XS D WKLUG YHUVLRQ +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | if (input.hasNextLine()) { | | | | String line = input.nextLine(); | | | | reverse(input); | | | | System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | | | | +------------------+ | +-----------------------------------------------+ 622 $JDLQ QRWLFH WKDW LW KDV LWV RZQ YDULDEOH FDOOHG OLQH WKDW LV LQGHSHQGHQW RI WKH RWKHU YDULDEOHV FDOOHG OLQH 7KLV YHUVLRQ RI WKH PHWKRG ILQGV WKDW WKHUH LV D OLQH WR UHYHUVH WKH WKLUG OLQH IXQ VR LW UHDGV LW LQ DQG UHDFKHV D UHFXUVLYH FDOO RQ UHYHUVH +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | if (input.hasNextLine()) { | | | | String line = input.nextLine(); | | | | --> reverse(input); | | | | System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | "fun" | | | +------------------+ | +-----------------------------------------------+ 7KLV EULQJV XS D IRXUWK YHUVLRQ RI WKH PHWKRG +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | +-----------------------------------------------+ | | | | public static void reverse(Scanner input) { | | | | | if (input.hasNextLine()) { | | | | | String line = input.nextLine(); | | | | | reverse(input); | +--| | | System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | | | | +------------------+ | +-----------------------------------------------+ 7KLV RQH ILQGV D IRXUWK OLQH RI LQSXW QR" VR LW UHDGV WKDW LQ DQG UHDFKHV WKH UHFXUVLYH FDOO 623 +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | +-----------------------------------------------+ | | | | public static void reverse(Scanner input) { | | | | | if (input.hasNextLine()) { | | | | | String line = input.nextLine(); | | | | | --> reverse(input); | +--| | | System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | "no?" | | | +------------------+ | +-----------------------------------------------+ 7KLV EULQJV XS D ILIWK YHUVLRQ RI WKH PHWKRG +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | +-----------------------------------------------+ | | | | public static void reverse(Scanner input) { | | | | | +-----------------------------------------------+ | | | | | public static void reverse(Scanner input) { | | | | | | if (input.hasNextLine()) { | +--| | | | String line = input.nextLine(); | | | | | reverse(input); | +--| | | System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | | | | +------------------+ | +-----------------------------------------------+ 7KLV RQH WXUQV RXW WR KDYH WKH HDV\ WDVN OLNH RXU ILQDO URERW WKDW ZDV DVNHG WR SULQW D OLQH RI VWDUV 7KLV WLPH DURXQG WKH 6FDQQHU LV HPSW\ LQSXW KDV1H[W/LQH UHWXUQV IDOVH 7KLV LV RXU YHU\ LPSRUWDQW EDVH FDVH WKDW VWRSV WKLV SURFHVV IURP JRLQJ RQ LQGHILQLWHO\ 7KLV YHUVLRQ RI WKH PHWKRG UHFRJQL]HV WKDW WKHUH DUH QR OLQHV WR UHYHUVH VR LW VLPSO\ WHUPLQDWHV 7KHQ ZKDW" :H UH GRQH ZLWK WKLV FDOO VR ZH WKURZ LW DZD\ DQG JR EDFN WR ZKHUH ZH ZHUH MXVW EHIRUH WKLV FDOO 624 +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | +-----------------------------------------------+ | | | | public static void reverse(Scanner input) { | | | | | if (input.hasNextLine()) { | | | | | String line = input.nextLine(); | | | | | reverse(input); | +--| | | --> System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | "no?" | | | +------------------+ | +-----------------------------------------------+ :H YH ILQLVKHG WKH FDOO RQ UHYHUVH VR QRZ ZH UH SRVLWLRQHG DW WKH SULQWOQ ULJKW DIWHU LW 6R ZH SULQW WKH WH[W LQ RXU YDULDEOH OLQH ZH SULQW QR" DQG WHUPLQDWH $QG ZKHUH GRHV WKDW OHDYH XV" 7KLV PHWKRG JRHV DZD\ DQG ZH UHWXUQ WR ZKHUH ZH ZHUH MXVW EHIRUH +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | +-----------------------------------------------+ | | | public static void reverse(Scanner input) { | | | | if (input.hasNextLine()) { | | | | String line = input.nextLine(); | | | | reverse(input); | | | | --> System.out.println(line); | | | | } | +--| | } | | | +------------------+ | +--| line | "fun" | | | +------------------+ | +-----------------------------------------------+ 6R ZH SULQW RXU OLQH RI WH[W ZKLFK LV IXQ DQG WKLV YHUVLRQ JRHV DZD\ +-----------------------------------------------+ | public static void reverse(Scanner input) { | | +-----------------------------------------------+ | | public static void reverse(Scanner input) { | | | if (input.hasNextLine()) { | | | String line = input.nextLine(); | | | reverse(input); | | | --> System.out.println(line); | | | } | | | } | | | +------------------+ | +--| line | "is" | | | +------------------+ | +-----------------------------------------------+ 625 6R ZH H[HFXWH WKLV SULQWOQ IRU WKH WH[W LV DQG HOLPLQDWH RQH PRUH FDOO +-----------------------------------------------+ | public static void reverse(Scanner input) { | | if (input.hasNextLine()) { | | String line = input.nextLine(); | | reverse(input); | | --> System.out.println(line); | | } | | } | | +------------------+ | | line | "this" | | | +------------------+ | +-----------------------------------------------+ 1RWLFH WKDW ZH YH ZULWWHQ RXW WKUHH OLQHV RI WH[W VR IDU no? fun is 6R RXU OHDS RI IDLWK ZDV MXVWLILHG 7KH UHFXUVLYH FDOO RQ UHYHUVH UHDG LQ WKH WKUHH OLQHV RI WH[W WKDW FDPH DIWHU WKH ILUVW OLQH RI LQSXW DQG SULQWHG WKHP LQ UHYHUVH RUGHU :H FRPSOHWH WKH WDVN E\ SULQWLQJ WKH ILUVW OLQH RI WH[W ZKLFK OHDGV WR WKLV RYHUDOO RXWSXW no? fun is this 7KHQ WKLV YHUVLRQ RI WKH PHWKRG WHUPLQDWHV DQG ZH UH GRQH 5HFXUVLYH )XQFWLRQV %RWK RI WKH H[DPSOHV ZH KDYH VHHQ VR IDU RI UHFXUVLRQ KDYH EHHQ DFWLRQ RULHQWHG PHWKRGV ZLWK D UHWXUQ W\SH RI YRLG ,Q WKLV VHFWLRQ ZH ZLOO H[DPLQH VRPH RI WKH LVVXHV WKDW FRPH XS ZKHQ \RX ZDQW WR ZULWH PHWKRGV WKDW FRPSXWH YDOXHV DQG UHWXUQ D UHVXOW ,QWHJHU ([SRQHQWLDWLRQ -DYD SURYLGHV D PHWKRG 0DWK SRZ WKDW DOORZV \RX WR FRPSXWH DQ H[SRQHQW ,I \RX ZDQW WR NQRZ ZKDW [\ LV HTXDO WR \RX FDQ FDOO 0DWK SRZ [ \ /HW V FRQVLGHU KRZ ZH FRXOG LPSOHPHQW WKH SRZ PHWKRG 7R NHHS WKLQJV VLPSOH ZH OO OLPLW RXUVHOYHV WR WKH GRPDLQ RI LQWHJHUV %HFDXVH ZH DUH OLPLWLQJ RXUVHOYHV WR LQWHJHUV ZH KDYH WR UHFRJQL]H DQ LPSRUWDQW SUHFRQGLWLRQ RI RXU PHWKRG :H ZRQ W EH DEOH WR FRPSXWH QHJDWLYH H[SRQHQWV EHFDXVH WKH UHVXOWV ZRXOG QRW EH LQWHJHUV 6R WKH PHWKRG ZH ZDQW WR ZULWH ZLOO ORRN OLNH WKLV // pre : y >= 0 // post: returns x^y public static int pow(int x, int y) { ... } 626 :H FRXOG REYLRXVO\ VROYH WKLV SUREOHP E\ ZULWLQJ D ORRS EXW ZH ZDQW WR H[SORUH KRZ WR ZULWH WKH PHWKRG UHFXUVLYHO\ $JDLQ WKH ULJKW SODFH WR VWDUW LV WR WKLQN DERXW GLIIHUHQW FDVHV :KDW ZRXOG EH WKH HDVLHVW H[SRQHQW WR FRPSXWH" ,W V SUHWW\ HDV\ WR FRPSXWH [ VR WKDW V D JRRG FDQGLGDWH EXW WKHUH LV DQ HYHQ PRUH EDVLF FDVH 7KH VLPSOHVW SRVVLEOH H[SRQHQW LV %\ GHILQLWLRQ DQ\ LQWHJHU WR WKH SRZHU LV FRQVLGHUHG WR EH 6R ZH FDQ EHJLQ RXU VROXWLRQ DV IROORZV public static int pow(int x, int y) { if (y == 0) { // base case with y == 0 return 1; } else { // recursive case with y > 0 ... } } ,Q WKH UHFXUVLYH FDVH ZH NQRZ WKDW \ LV JUHDWHU WKDQ ,Q RWKHU ZRUGV WKHUH ZLOO EH DW OHDVW RQH IDFWRU RI [ WR EH LQFOXGHG LQ WKH UHVXOW :H NQRZ IURP PDWKHPDWLFV WKDW [\ [ [\ 7KLV HTXDWLRQ H[SUHVVHV [ WR WKH \ SRZHU LQ WHUPV RI [ WR D VPDOOHU SRZHU \ 7KHUHIRUH LW FDQ VHUYH DV RXU UHFXUVLYH FDVH $OO ZH KDYH WR LV WR WUDQVODWH LW LQWR LWV -DYD HTXLYDOHQW public static int pow(int if (y == 0) { // base case with return 1; } else { // recursive case return x * pow(x, } } x, int y) { y == 0 with y > 0 y - 1); 7KLV LV D FRPSOHWH UHFXUVLYH VROXWLRQ 7UDFLQJ WKH H[HFXWLRQ RI D UHFXUVLYH IXQFWLRQ LV D OLWWOH PRUH GLIILFXOW WKDQ ZLWK D YRLG PHWKRG EHFDXVH ZH KDYH WR NHHS WUDFN RI WKH YDOXHV EHLQJ UHWXUQHG E\ HDFK UHFXUVLYH FDOO 7KH IROORZLQJ LV D WUDFH RI H[HFXWLRQ VKRZLQJ KRZ ZH ZRXOG FRPSXWH pow(3, | | | | | | | | | pow(3, 5) = 3 pow(3, | | | | | | | pow(3, 5) = 3 * pow(3, 4) 4) = 3 * pow(3, 3) pow(3, 3) = 3 * pow(3, 2) | pow(3, 2) = 3 * pow(3, 1) | | pow(3, 1) = 3 * pow(3, 0) | | | pow(3, 0) = 1 | | pow(3, 1) = 3 * 1 = 3 | pow(3, 2) = 3 * 3 = 9 pow(3, 3) = 3 * 9 = 27 4) = 3 * 27 = 81 * 81 = 243 1RWLFH WKDW ZH PDNH D VHULHV RI VL[ UHFXUVLYH FDOOV LQ D URZ XQWLO ZH JHW WR WKH EDVH FDVH RI FRPSXWLQJ WR WKH SRZHU 7KDW FDOO UHWXUQV WKH YDOXH DQG WKHQ WKH UHFXUVLRQ XQZLQGV FRPSXWLQJ WKH YDULRXV DQVZHUV DV LW UHWXUQV IURP HDFK PHWKRG FDOO 627 ,W LV XVHIXO WR WKLQN DERXW ZKDW ZLOO KDSSHQ LI VRPHRQH YLRODWHV WKH SUHFRQGLWLRQ E\ DVNLQJ IRU D QHJDWLYH H[SRQHQW )RU H[DPSOH ZKDW LI VRPHRQH DVNV IRU SRZ 7KH PHWKRG ZRXOG UHFXUVLYHO\ DVN IRU SRZ ZKLFK ZRXOG DVN IRU SRZ ZKLFK ZRXOG DVN IRU SRZ DQG VR RQ ,Q RWKHU ZRUGV LW OHDGV WR DQ LQILQLWH UHFXUVLRQ ,Q VRPH VHQVH WKLV LV RND\ EHFDXVH VRPHRQH FDOOLQJ WKH PHWKRG VKRXOG SD\ DWWHQWLRQ WR WKH SUHFRQGLWLRQ %XW LW V QRW PXFK ZRUN IRU XV WR KDQGOH WKLV FDVH LQ D PRUH HOHJDQW PDQQHU 2XU VROXWLRQ LV VWUXFWXUHG DV D VHULHV RI FDVHV VR ZH FDQ VLPSO\ DGG D QHZ FDVH IRU LOOHJDO H[SRQHQWV public static int pow(int x, int y) { if (y < 0) { throw new IllegalArgumentException("negative exponent"); } else if (y == 0) { // base case with y == 0 return 1; } else { // recursive case with y > 0 return x * pow(x, y - 1); } } 2QH RI WKH DGYDQWDJHV RI ZULWLQJ IXQFWLRQV UHFXUVLYHO\ LV WKDW LI ZH FDQ LGHQWLI\ RWKHU FDVHV ZH FDQ SRWHQWLDOO\ PDNH WKH IXQFWLRQ PRUH HIILFLHQW )RU H[DPSOH VXSSRVH WKDW \RX ZDQW WR FRPSXWH ,Q LWV FXUUHQW IRUP WKH PHWKRG ZLOO PXOWLSO\ E\ E\ D WRWDO RI WLPHV :H FDQ GR EHWWHU WKDQ WKDW ,Q SDUWLFXODU ZH NQRZ WKDW LI \ LV DQ HYHQ H[SRQHQW WKHQ [\ [ \ ZH FDQ FRPSXWH 7KLV FDVH FDQ EH DGGHG WR RXU PHWKRG LQ D 6R LQVWHDG RI FRPSXWLQJ UHODWLYHO\ VLPSOH PDQQHU public static int pow(int x, int y) { if (y < 0) { throw new IllegalArgumentException("negative exponent"); } else if (y == 0) { // base case with y == 0 return 1; } else if (y % 2 == 0) { // recursive case with y > 0, y even return pow(x * x, y / 2); } else { // recursive case with y > 0, y odd return x * pow(x, y - 1); } } 7KLV YHUVLRQ RI WKH PHWKRG LV PRUH HIILFLHQW WKDQ WKH RULJLQDO 7KH IROORZLQJ LV D WUDFH RI H[HFXWLRQ IRU FRPSXWLQJ 628 pow(2, | | | | | | | | | pow(2, 16) = pow(4, 8) pow(4, 8) = pow(16, 4) | pow(16, 4) = pow(256, 2) | | pow(256, 2) = pow(65536, 1) | | | pow(65536, 1) = 65536 * pow(65536, 0) | | | | pow(65536, 0) = 1 | | | pow(65536, 1) = 65536 * 1 = 65536 | | pow(256, 2) = 65536 | pow(16, 4) = 65536 pow(4, 8) = 65536 16) = 65536 *UHDWHVW &RPPRQ 'LYLVRU ,Q 0DWKHPDWLFV ZH RIWHQ ZDQW WR NQRZ WKH ODUJHVW LQWHJHU WKDW JRHV HYHQO\ LQWR WZR GLIIHUHQW LQWHJHUV ZKLFK LV NQRZQ DV WKH JUHDWHVW FRPPRQ GLYLVRU RU JFG RI WKH WZR LQWHJHUV /HW V H[SORUH KRZ WR ZULWH WKH JFG PHWKRG UHFXUVLYHO\ )RU QRZ OHW V QRW ZRUU\ DERXW QHJDWLYH YDOXHV RI [ DQG \ 6R ZH ZDQW WR ZULWH WKLV PHWKRG // pre : x >= 0, y >= 0 // post: returns the greatest common divisor of x and y public static int gcd(int x, int y) { ... } 7R LQWURGXFH VRPH YDULHW\ OHW V WU\ WR ILJXUH RXW WKH UHFXUVLYH FDVH ILUVW DQG WKHQ ILJXUH RXW WKH EDVH FDVH 6XSSRVH IRU H[DPSOH WKDW ZH DUH DVNHG WR FRPSXWH WKH JFG RI DQG 7KH DQVZHU LV EHFDXVH JRHV HYHQO\ LQWR HDFK QXPEHU DQG LW LV WKH ODUJHVW LQWHJHU WKDW JRHV HYHQO\ LQWR ERWK 7KHUH DUH PDQ\ ZD\V WR FRPSXWH WKH JFG RI WZR QXPEHUV 2QH RI WKH PRVW HIILFLHQW DOJRULWKPV GDWHV EDFN DW OHDVW WR WKH WLPH RI (XFOLG DQG SHUKDSV HYHQ IDUWKHU 7KH LGHD LV WR HOLPLQDWH DQ\ PXOWLSOHV RI WKH VPDOOHU LQWHJHU IURP WKH ODUJHU LQWHJHU )RU RXU H[DPSOH RI DQG ZH NQRZ WKDW 132 = 20 * 6 + 12 7KHUH DUH PXOWLSOHV RI LQ ZLWK D UHPDLQGHU RI (XFOLG V DOJRULWKP VD\V WKDW ZH FDQ LJQRUH WKH PXOWLSOHV RI DQG MXVW IRFXV RQ WKH YDOXH ,Q RWKHU ZRUGV ZH NQRZ WKDW ZH FDQ UHSODFH ZLWK gcd(132, 20) = gcd(12, 20) 7KH SURRI RI WKLV SULQFLSOH LV EH\RQG WKH VFRSH RI WKLV ERRN EXW WKDW LV WKH EDVLF LGHD 7KLV LV HDV\ WR H[SUHVV LQ -DYD WHUPV EHFDXVH WKH PRG RSHUDWRU JLYHV XV WKH UHPDLQGHU ZKHQ RQH QXPEHU LV GLYLGHG E\ DQRWKHU ([SUHVVLQJ WKLV SULQFLSOH LQ JHQHUDO WHUPV ZH NQRZ WKDW gcd(x, y) = gcd(x % y, y) when y > 0 $JDLQ WKH SURRI LV EH\RQG WKH VFRSH RI WKLV ERRN EXW JLYHQ WKLV EDVLF SULQFLSOH ZH FDQ SURGXFH D UHFXUVLYH VROXWLRQ WR WKH SUREOHP 6R ZH PLJKW WU\ WR ZULWH WKH PHWKRG DV IROORZV 629 public static int gcd(int x, int y) { if (...) { // base case ... } else { // recursive case return gcd(x % y, y); } } 7KLV LVQ W D EDG ILUVW DWWHPSW EXW LW KDV D SUREOHP ,W V QRW HQRXJK WR EH PDWKHPDWLFDOO\ FRUUHFW :H KDYH WR NQRZ WKDW RXU UHFXUVLYH VROXWLRQ NHHSV UHGXFLQJ WKH RYHUDOO SUREOHP WR D VLPSOHU SUREOHP ,I ZH VWDUW ZLWK DQG WKH PHWKRG PDNHV SURJUHVV RQ WKH ILUVW FDOO EXW WKHQ LW VWDUWV UHSHDWLQJ LWVHOI gcd(132, 20) = gcd(12, 20) gcd(12, 20) = gcd(12, 20) gcd(12, 20) = gcd(12, 20) gcd(12, 20) = gcd(12, 20) ... 7KLV ZLOO OHDG WR LQILQLWH UHFXUVLRQ 7KH (XFOLGHDQ WULFN KHOSHG WKH ILUVW WLPH DURXQG EHFDXVH IRU WKH ILUVW FDOO [ LV JUHDWHU WKDQ \ LV JUHDWHU WKDQ 6R WKH DOJRULWKP PDNHV SURJUHVV RQO\ LI WKH ILUVW QXPEHU LV ODUJHU WKDQ WKH VHFRQG QXPEHU 7KH OLQH RI FRGH WKDW LV FDXVLQJ XV SUREOHPV LV WKLV RQH return gcd(x % y, y); :KHQ ZH FRPSXWH [ \ ZH DUH JXDUDQWHHG WR JHW D UHVXOW WKDW LV VPDOOHU WKDQ \ 7KDW PHDQV WKDW RQ WKH UHFXUVLYH FDOO WKH ILUVW YDOXH ZLOO DOZD\V EH VPDOOHU WKDQ WKH VHFRQG YDOXH 7R PDNH WKH DOJRULWKP ZRUN ZH QHHG WKH RSSRVLWH WR EH WUXH $QG ZH FDQ PDNH WKH RSSRVLWH EH WUXH VLPSO\ E\ UHYHUVLQJ WKH RUGHU RI WKH DUJXPHQWV return gcd(y, x % y); 2Q WKLV FDOO ZH DUH JXDUDQWHHG WR KDYH D ILUVW YDOXH WKDW LV ODUJHU WKDQ WKH VHFRQG YDOXH ,I ZH WUDFH WKLV YHUVLRQ RI WKH PHWKRG IRU FRPSXWLQJ WKH JFG RI DQG ZH JHW WKH IROORZLQJ gcd(132, 20) = gcd(20, 12) gcd(20, 12) = gcd(12, 8) gcd(12, 8) = gcd(8, 4) gcd(8, 4) = gcd(4, 0) ... $W WKLV SRLQW ZH KDYH WR GHFLGH ZKDW WKH JFG RI DQG LV ,W PD\ VHHP VWUDQJH EXW WKH DQVZHU LV WKDW WKH JFG RI DQG LV ,Q JHQHUDO JFG Q LV DOZD\V Q 2EYLRXVO\ WKH JFG FRXOGQ W EH DQ\ ODUJHU WKDQ Q DQG Q JRHV HYHQO\ LQWR Q %XW Q DOVR JRHV HYHQO\ LQWR EHFDXVH FDQ EH ZULWWHQ DV DQ HYHQ PXOWLSOH RI Q Q 7KLV REVHUYDWLRQ OHDGV XV WR WKH EDVH FDVH ,I \ LV WKHQ WKH JFG LV [ 630 public static int gcd(int if (y == 0) { // base case with return x; } else { // recursive case return gcd(y, x % } } x, int y) { y == 0 with y > 0 y); :LWK WKLV EDVH FDVH ZH DOVR VROYH WKH SRWHQWLDO SUREOHP WKDW WKH (XFOLGHDQ IRUPXOD GHSHQGV RQ \ QRW EHLQJ :H VWLOO KDYH WR WKLQN DERXW WKH FDVH ZKHUH RQH RU ERWK RI [ DQG \ LV QHJDWLYH :H FRXOG NHHS WKH SUHFRQGLWLRQ DQG WKURZ DQ H[FHSWLRQ ZKHQ RQH RU ERWK RI WKH YDOXHV LV QHJDWLYH ,W LV PRUH FRPPRQ LQ 0DWKHPDWLFV WR UHWXUQ WKH JFG RI WKH DEVROXWH YDOXH RI WKH WZR YDOXHV :H FDQ DFFRPSOLVK WKLV ZLWK RQH H[WUD FDVH IRU QHJDWLYHV public static int gcd(int x, int y) { if (x < 0 || y < 0) { // recursive case with negative value(s) return gcd(Math.abs(x), Math.abs(y)); } else if (y == 0) { // base case with y == 0 return x; } else { // recursive case with y > 0 return gcd(y, x % y); } } &RPPRQ 3URJUDPPLQJ (UURU ,QILQLWH 5HFXUVLRQ (YHU\RQH ZKR XVHV UHFXUVLRQ WR ZULWH SURJUDPV HYHQWXDOO\ DFFLGHQWDOO\ ZULWHV D VROXWLRQ WKDW OHDGV WR LQILQLWH UHFXUVLRQ )RU H[DPSOH EHORZ LV D VOLJKW YDULDWLRQ RI WKH JFG PHWKRG WKDW GRHVQ W ZRUN public static int gcd(int x, int y) { if (x <= 0 || y <= 0) { // recursive case with negative value(s) return gcd(Math.abs(x), Math.abs(y)); } else if (y == 0) { // base case with y == 0 return x; } else { // recursive case with y > 0 return gcd(y, x % y); } } 7KLV VROXWLRQ LV MXVW VOLJKWO\ GLIIHUHQW WKDQ WKH RQH ZH ZURWH ,Q WKH WHVW IRU QHJDWLYH YDOXHV WKLV FRGH WHVWV ZKHWKHU [ DQG \ DUH OHVV WKDQ RU HTXDO WR 7KH RULJLQDO FRGH WHVWV ZKHWKHU WKH\ DUH VWULFWO\ OHVV WKDQ ,W GRHVQ W VHHP OLNH WKLV VKRXOG PDNH PXFK RI D GLIIHUHQFH EXW LW GRHV ,I ZH H[HFXWH WKLV YHUVLRQ RI WKH FRGH RQ RXU RULJLQDO SUREOHP RI ILQGLQJ WKH JFG RI DQG WKH SURJUDP SURGXFHV PDQ\ OLQHV RI RXWSXW WKDW ORRN OLNH WKLV 631 at at at at at at at Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) Bug.gcd(Bug.java:9) 7KH ILUVW WLPH \RX VHH WKLV \RX DUH OLNHO\ WR WKLQN WKDW VRPHWKLQJ KDV EURNHQ RQ \RXU FRPSXWHU EHFDXVH \RX ZLOO JHW VR PDQ\ OLQHV RI RXWSXW 7KH QXPEHU RI OLQHV RI RXWSXW ZLOO YDU\ IURP RQH V\VWHP WR DQRWKHU EXW LW V OLNHO\ WR EH KXQGUHGV RI OLQHV RI RXWSXW DQG LQ VRPH FDVHV WKRXVDQGV RI OLQHV RI RXWSXW ,I \RX VFUROO DOO WKH ZD\ EDFN XS \RX OO VHH WKDW LW EHJLQV ZLWK WKLV PHVVDJH Exception in thread "main" java.lang.StackOverflowError at Bug.gcd(Bug.java:9) at Bug.gcd(Bug.java:9) at Bug.gcd(Bug.java:9) ... -DYD LV OHWWLQJ \RX NQRZ WKDW WKH FDOO VWDFN KDV JRWWHQ WRR ELJ :K\ LV WKLV KDSSHQLQJ" 5HPHPEHU WKH WUDFH RI H[HFXWLRQ IRU WKLV FDVH gcd(132, 20) = gcd(20, 12) gcd(20, 12) = gcd(12, 8) gcd(12, 8) = gcd(8, 4) gcd(8, 4) = gcd(4, 0) ... 7KLQN RI ZKDW KDSSHQV DW WKLV SRLQW ZKHQ ZH FDOO JFG 7KH YDOXH RI \ LV ZKLFK LV RXU EDVH FDVH 6R QRUPDOO\ ZH ZRXOG H[SHFW WKH PHWKRG WR UHWXUQ WKH YDOXH DQG WHUPLQDWH %XW WKH PHWKRG EHJLQV E\ FKHFNLQJ ZKHWKHU HLWKHU RI [ RU \ LV OHVV WKDQ RU HTXDO WR 6LQFH \ LV WKLV WHVW HYDOXDWHV WR WUXH 6R ZH PDNH D UHFXUVLYH FDOO ZLWK WKH DEVROXWH YDOXH RI [ DQG \ %XW WKH DEVROXWH YDOXHV RI DQG DUH DQG ,Q RWKHU ZRUGV ZH GHFLGH WKDW WKH JFG PXVW EH HTXDO WR WKH JFG ZKLFK PXVW EH HTXDO WR WKH JFG gcd(132, 20) = gcd(20, 12) gcd(20, 12) = gcd(12, 8) gcd(12, 8) = gcd(8, 4) gcd(8, 4) = gcd(4, 0) gcd(4, 0) = gcd(4, 0) gcd(4, 0) = gcd(4, 0) gcd(4, 0) = gcd(4, 0) gcd(4, 0) = gcd(4, 0) ... ,Q RWKHU ZRUGV WKLV YHUVLRQ JHQHUDWHV LQILQLWHO\ PDQ\ UHFXUVLYH FDOOV 7KLQN LQ WHUPV RI WKH SLHFHV RI SDSHU WKDW DUH VWDFNHG RQ WRS RI HDFK RWKHU DV PHWKRGV DUH FDOOHG -DYD DOORZV \RX WR PDNH D ORW RI UHFXUVLYH FDOOV EXW HYHQWXDOO\ LW UXQV RXW RI VSDFH :KHQ LW GRHV LW JLYHV \RX D EDFNWUDFH WR OHW \RX NQRZ KRZ \RX JRW WR WKH HUURU ,Q WKLV FDVH WKH EDFNWUDFH LV QRW QHDUO\ DV KHOSIXO DV XVXDO EHFDXVH DOPRVW DOO RI WKH FDOOV ZLOO LQYROYH WKH LQILQLWH UHFXUVLRQ 7R KDQGOH WKHVH VLWXDWLRQV \RX KDYH WR ORRN FORVHO\ DW WKH OLQH QXPEHU WR VHH ZKLFK OLQH RI \RXU SURJUDP JHQHUDWHG WKH LQILQLWH UHFXUVLRQ ,Q WKLV FDVH ZH G NQRZ WKDW LW LV WKH UHFXUVLYH FDOO IRU QHJDWLYH [ DQG \ YDOXHV 7KDW DORQH PLJKW EH HQRXJK WR DOORZ XV WR SLQSRLQW WKH HUURU ,I QRW \RX PLJKW QHHG WR LQFOXGH SULQWOQ VWDWHPHQWV WR ILJXUH RXW ZKDW LV JRLQJ RQ )RU H[DPSOH LQ WKLV FRGH ZH FRXOG DGG D SULQWOQ MXVW EHIRUH WKH UHFXUVLYH FDOO 632 public static int gcd(int x, int y) { if (x <= 0 || y <= 0) { // recursive case with negative value(s) System.out.println("x = " + x + " and y = " + y); return gcd(Math.abs(x), Math.abs(y)); } else if (y == 0) { ... } :LWK WKDW SULQWOQ LQ SODFH WKH FRGH SURGXFHV KXQGUHGV RI OLQHV RI RXWSXW RI WKH IRUP x = 4 and y = 0 ,I ZH H[DPLQH WKDW FDVH FORVHO\ ZH G VHH WKDW ZH GRQ W KDYH QHJDWLYH YDOXHV DQG ZH G UHDOL]H WKDW ZH KDYH WR IL[ WKH WHVW ZH DUH XVLQJ 5HFXUVLYH *UDSKLFV RSWLRQDO 7KHUH KDV EHHQ D JUHDW GHDO RI LQWHUHVW LQ WKH SDVW \HDUV DERXW DQ HPHUJLQJ ILHOG RI 0DWKHPDWLFV FDOOHG IUDFWDO JHRPHWU\ $ IUDFWDO LV D JHRPHWULF REMHFW WKDW LV UHFXUVLYHO\ FRQVWUXFWHG RU VHOI VLPLODU $ IUDFWDO VKDSH FRQWDLQV VPDOOHU YHUVLRQV RI LWVHOI VR WKDW LW ORRNV VLPLODU DW DOO PDJQLILFDWLRQV %HQRLW 0DQGHOEURW FUHDWHG WKH IUDFWDO ILHOG LQ ZLWK KLV ILUVW SXEOLFDWLRQ DERXW IUDFWDOV LQ JHQHUDO DQG DERXW D VSHFLILF IUDFWDO WKDW KDV FRPH WR EH NQRZQ DV WKH 0DQGHOEURW VHW 7KH PRVW LPSUHVVLYH DVSHFW RI IUDFWDO JHRPHWU\ LV WKDW H[WUHPHO\ LQWULFDWH DQG FRPSOH[ SKHQRPHQD FDQ EH GHVFULEHG ZLWK D VLPSOH VHW RI UXOHV :KHQ 0DQGHOEURW DQG RWKHUV EHJDQ GUDZLQJ SLFWXUHV RI WKHLU IUDFWDOV WKH\ ZHUH DQ LQVWDQW KLW 0DQ\ IUDFWDOV FDQ EH GHVFULEHG HDVLO\ ZLWK UHFXUVLRQ $V DQ H[DPSOH ZH ZLOO H[SORUH D UHFXUVLYH PHWKRG IRU GUDZLQJ ZKDW LV NQRZQ DV WKH 6LHUSLQVNL WULDQJOH :H FDQ W GUDZ WKH DFWXDO IUDFWDO EHFDXVH LW LV FRPSRVHG RI LQILQLWHO\ PDQ\ VXEWULDQJOHV ,QVWHDG ZH ZLOO ZULWH D PHWKRG WKDW SURGXFHV YDULRXV OHYHOV WKDW DSSUR[LPDWH WKH DFWXDO IUDFWDO $W OHYHO ZH GUDZ DQ HTXLODWHUDO WULDQJOH 633 ,Q JRLQJ WR OHYHO ZH GUDZ WKUHH VPDOOHU WULDQJOHV WKDW DUH FRQWDLQHG ZLWKLQ WKH RULJLQDO WULDQJOH :H DSSO\ WKLV SULQFLSOH LQ D UHFXUVLYH PDQQHU -XVW DV ZH UHSODFHG WKH RULJLQDO WULDQJOH ZLWK WKUHH LQQHU WULDQJOHV ZH UHSODFH HDFK RI WKHVH WKUHH WULDQJOHV ZLWK WKUHH LQQHU WULDQJOHV WR REWDLQ D ILJXUH ZLWK QLQH WULDQJOHV WR JHW WR OHYHO 7KLV SURFHVV FRQWLQXHV LQGHILQLWHO\ PDNLQJ D PRUH LQWULFDWH SDWWHUQ DW HDFK QHZ OHYHO $W OHYHO JHW WKH IROORZLQJ ZH 634 :H FDQ XVH WKH 'UDZLQJ3DQHO FODVV IURP &KDSWHU WR VROYH WKLV SUREOHP :H FDQ SDVV WKH *UDSKLFV REMHFW IRU WKH SDQHO WR WKH PHWKRG WKDW LV WR GUDZ WKH WULDQJOHV 7KH PHWKRG ZLOO DOVR QHHG WR NQRZ WKH OHYHO WR XVH DQG ZLOO QHHG WR NQRZ WKH WKUHH YHUWLFHV RI WKH WULDQJOH ZKLFK ZH FDQ SDVV DV SRLQW REMHFWV 6R RXU PHWKRG ZLOO ORRN OLNH WKLV public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { ... } 2XU EDVH FDVH ZLOO EH WR GUDZ WKH EDVLF WULDQJOH IRU OHYHO 7KH *UDSKLFV FODVV KDV PHWKRGV IRU ILOOLQJ UHFWDQJOHV DQG RYDOV EXW QRW IRU ILOOLQJ WULDQJOHV )RUWXQDWHO\ WKHUH LV D 3RO\JRQ FODVV LQ WKH MDYD DZW SDFNDJH <RX FRQVWUXFW D 3RO\JRQ DQG DGG D VHULHV RI SRLQWV WR LW 7KH *UDSKLFV FODVV KDV D PHWKRG FDOOHG ILOO3RO\JRQ WKDW ZH FDQ XVH WR ILOO WKH SRO\JRQ RQFH ZH YH VSHFLILHG WKH WKUHH YHUWLFHV 6R RXU EDVH FDVH ZLOO ORRN OLNH WKLV public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles ... } } 0RVW RI WKH ZRUN KDSSHQV LQ WKH UHFXUVLYH FDVH :H KDYH WR VSOLW WKH WULDQJOH LQWR WKUHH VPDOOHU WULDQJOHV ,I ZH ODEHO WKH YHUWLFHV RI WKH RYHUDOO WULDQJOH DV IROORZV 635 p2 * /-\ /---\ /-----\ /-------\ /---------\ /-----------\ /-------------\ /---------------\ /-----------------\ p1 *===================* p3 :H QHHG WR FRPSXWH WKUHH QHZ SRLQWV WKDW DUH WKH PLGSRLQWV RI WKH WKUHH VLGHV RI WKHVH WULDQJOHV p2 * /-\ /---\ /-----\ /-------\ p4 *=========* p5 /-\ /-\ /---\ /---\ /-----\ /-----\ /-------\ /-------\ p1 *=========*=========* p3 p6 7KHUH DUH WKUHH GLIIHUHQW PLGSRLQW FRPSXWDWLRQV LQYROYHG KHUH VR LW VHHPV FOHDU WKDW LW ZRXOG EH KHOSIXO WR ILUVW ZULWH D PHWKRG WKDW ZLOO FRPSXWH WKH PLGSRLQW RI D VHJPHQW JLYHQ WZR HQGSRLQWV public static Point midpoint(Point p1, Point p2) { return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } *LYHQ WKLV PHWKRG ZH FDQ HDVLO\ FRPSXWH WKH WKUHH QHZ SRLQWV /RRNLQJ DW WKH GLDJUDP DERYH ZH VHH WKDW S LV WKH PLGSRLQW RI S DQG S S LV WKH PLGSRLQW RI S DQG S S LV WKH PLGSRLQW RI S DQG S 2QFH ZH KDYH FRPSXWHG WKRVH SRLQWV ZH FDQ GHVFULEH WKH VPDOOHU WULDQJOHV DV IROORZV LQ WKH ORZHU OHIW LV WKH WULDQJOH IRUPHG E\ S S DQG S RQ WRS LV WKH WULDQJOH IRUPHG E\ S S DQG S LQ WKH ORZHU ULJKW LV WKH WULDQJOH IRUPHG E\ S S DQG S 7KH ILQDO GHWDLO ZH KDYH WR WKLQN DERXW LV WKH OHYHO ,I \RX ORRN DJDLQ DW WKH OHYHO YHUVLRQ RI WKH ILJXUH \RX ZLOO QRWLFH WKDW LW LV FRPSRVHG RI WKUHH VLPSOH WULDQJOHV ,Q RWKHU ZRUGV WKH OHYHO ILJXUH LV FRPSRVHG RI WKUHH OHYHO ILJXUHV 6LPLODUO\ WKH OHYHO ILJXUH LV FRPSRVHG RI WKUHH OHYHO ILJXUHV ZKLFK LQ WXUQ DUH HDFK FRPSRVHG RI WKUHH OHYHO ILJXUHV ,Q JHQHUDO LI ZH KDYH EHHQ DVNHG WR GUDZ D OHYHO Q ILJXUH ZH GR VR E\ GUDZLQJ WKUHH OHYHO Q ILJXUHV 636 %\ WXUQLQJ WKHVH REVHUYDWLRQV LQWR FRGH ZH FDQ FRPSOHWH WKH UHFXUVLYH PHWKRG public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles Point p4 = midpoint(p1, p2); Point p5 = midpoint(p2, p3); Point p6 = midpoint(p1, p3); // recurse on 3 triangular areas drawFigure(level - 1, g, p1, p4, p6); drawFigure(level - 1, g, p4, p2, p5); drawFigure(level - 1, g, p6, p5, p3); } } 7KHUH LV D OLPLW WR KRZ PDQ\ OHYHOV GHHS ZH FDQ JR 7KH 'UDZLQJ3DQHO KDV D ILQLWH UHVROXWLRQ VR DW VRPH SRLQW ZH ZRQ W EH DEOH WR VXEGLYLGH RXU WULDQJOHV DQ\ IXUWKHU $OVR DW HDFK QHZ OHYHO WKH QXPEHU RI WULDQJOHV WKDW ZH GUDZ WULSOHV ZKLFK PHDQV WKDW WKH QXPEHU RI WULDQJOHV LQFUHDVHV H[SRQHQWLDOO\ ZLWK WKH OHYHO %HORZ LV D FRPSOHWH SURJUDP WKDW DOORZV WKH XVHU WR GHFLGH ZKDW OHYHO WR XVH LQ GUDZLQJ WKH ILJXUH 637 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import java.awt.*; import java.util.*; public class Sierpinski { public static final int SIZE = 256; public static void main(String args) { // prompt for level Scanner console = new Scanner(System.in); System.out.print("What level do you want? "); int level = console.nextInt(); // initialize drawing panel DrawingPanel p = new DrawingPanel(SIZE, SIZE); p.setBackground(Color.CYAN); Graphics g = p.getGraphics(); // compute triangle endpoints and begin recursion int triangleHeight = (int) Math.round(SIZE * Math.sqrt(3.0) / 2.0); Point p1 = new Point(0, triangleHeight); Point p2 = new Point(SIZE / 2, 0); Point p3 = new Point(SIZE, triangleHeight); drawFigure(level, g, p1, p2, p3); } // Draws a Sierpinski fractal to the given level inside the triangle // whose vertices are (p1, p2, p3). public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles Point p4 = midpoint(p1, p2); Point p5 = midpoint(p2, p3); Point p6 = midpoint(p1, p3); // recurse on 3 triangular areas drawFigure(level - 1, g, p1, p4, p6); drawFigure(level - 1, g, p4, p2, p5); drawFigure(level - 1, g, p6, p5, p3); } } // returns the midpoint of p1 and p2 public static Point midpoint(Point p1, Point p2) { return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } } 638 &DVH 6WXG\ 3UHIL[ (YDOXDWRU ,Q WKLV VHFWLRQ ZH ZLOO H[SORUH WKH XVH RI UHFXUVLRQ WR HYDOXDWH FRPSOH[ QXPHULF H[SUHVVLRQV :H ZLOO EHJLQ E\ H[SORULQJ WKH GLIIHUHQW FRQYHQWLRQV IRU VSHFLI\LQJ QXPHULF H[SUHVVLRQV DQG WKHQ ZH ZLOO VHH KRZ UHFXUVLRQ PDNHV LW UHODWLYHO\ HDV\ WR LPSOHPHQW RQH RI WKH VWDQGDUG FRQYHQWLRQV ,QIL[ 3UHIL[ DQG 3RVWIL[ 1RWDWLRQ :KHQ ZH ZULWH QXPHULF H[SUHVVLRQV LQ D -DYD SURJUDP ZH SXW QXPHULF RSHUDWRUV OLNH EHWZHHQ WKH WZR RSHUDQGV DV LQ 3.5 + 8.2 9.1 * 12.7 7.8 * (2.3 + 2.5) DQG 3XWWLQJ WKH RSHUDWRU EHWZHHQ WKH RSHUDQGV LV D FRQYHQWLRQ NQRZQ DV LQIL[ QRWDWLRQ $ VHFRQG FRQYHQWLRQ LV WR SXW WKH RSHUDWRU LQ IURQW RI WKH WZR RSHUDQGV DV LQ + 3.5 8.2 * 9.1 12.7 * 7.8 + 2.3 2.5 3XWWLQJ WKH RSHUDWRU LQ IURQW RI WKH RSHUDQGV LV D FRQYHQWLRQ NQRZQ DV SUHIL[ QRWDWLRQ 3UHIL[ QRWDWLRQ ORRNV RGG IRU V\PEROV OLNH DQG EXW LW LV PRUH OLNH PDWKHPDWLFDO IXQFWLRQ QRWDWLRQ ZKHUH WKH QDPH RI WKH IXQFWLRQ JRHV ILUVW )RU H[DPSOH LI LQVWHDG RI XVLQJ WKH RSHUDWRUV ZH LQVWHDG ZHUH FDOOLQJ PHWKRGV ZH ZRXOG ZULWH plus(3.5, 8.2) times(9.1, 12.7) times(7.8, plus(2.3, 2.5)) 7KHUH LV D WKLUG FRQYHQWLRQ NQRZQ DV SRVWIL[ QRWDWLRQ ZKHUH WKH RSHUDWRU DSSHDUV DIWHU WKH WZR RSHUDQGV DV LQ 3.5 8.2 + 9.1 12.7 * 7.8 2.3 2.5 + * 3RVWIL[ QRWDWLRQ LV DOVR NQRZQ DV 5HYHUVH 3ROLVK 1RWDWLRQ RU 531 )RU PDQ\ \HDUV +HZOHWW 3DFNDUG KDV VROG VFLHQWLILF FDOFXODWRUV WKDW XVH 531 UDWKHU WKDQ QRUPDO LQIL[ QRWDWLRQ :H DUH VR XVHG WR LQIL[ QRWDWLRQ WKDW LW WDNHV D ZKLOH WR OHDUQ WKH RWKHU WZR FRQYHQWLRQV 2QH RI WKH LQWHUHVWLQJ IDFWV \RX ZLOO GLVFRYHU LI \RX WDNH WKH WLPH WR OHDUQ WKH RWKHU FRQYHQWLRQV LV WKDW LQIL[ LV WKH RQO\ QRWDWLRQ WKDW UHTXLUHV SDUHQWKHVHV 7KH RWKHU WZR QRWDWLRQV DUH XQDPELJXRXV %HORZ LV D WDEOH WKDW VXPPDUL]HV WKH WKUHH QRWDWLRQV 639 $ULWKPHWLF 1RWDWLRQV 1RWDWLRQ LQIL[ 'HVFULSWLRQ RSHUDWRU EHWZHHQ RSHUDQGV ([DPSOHV SUHIL[ RSHUDWRU EHIRUH RSHUDQGV IXQFWLRQDO QRWDWLRQ SRVWIL[ RSHUDWRU DIWHU RSHUDQGV 5HYHUVH 3ROLVK 1RWDWLRQ 3UHIL[ (YDOXDWRU 2I WKH WKUHH VWDQGDUG QRWDWLRQV SUHIL[ QRWDWLRQ LV WKH RQH PRVW HDVLO\ LPSOHPHQWHG ZLWK UHFXUVLRQ ,Q WKLV VHFWLRQ ZH ZLOO ZULWH D PHWKRG WKDW UHDGV D SUHIL[ H[SUHVVLRQ IURP D 6FDQQHU DQG WKDW FRPSXWHV LWV YDOXH 6R RXU PHWKRG VKRXOG ORRN OLNH WKLV // pre : input contains a legal prefix expression // post: expression is consumed and the result is returned public static double evaluate(Scanner input) { ... } %HIRUH ZH FDQ EHJLQ ZH KDYH WR FRQVLGHU ZKDW NLQG RI LQSXW ZH DUH JRLQJ WR JHW $V WKH SUHFRQGLWLRQ LQGLFDWHV ZH ZLOO DVVXPH WKDW WKH 6FDQQHU FRQWDLQV D OHJDO SUHIL[ H[SUHVVLRQ 7KH VLPSOHVW SRVVLEOH H[SUHVVLRQ ZRXOG EH D QXPEHU DV LQ 38.9 7KHUH LVQ W PXFK RI DQ H[SUHVVLRQ WR HYDOXDWH LQ WKLV FDVH :H FDQ VLPSO\ UHDG DQG UHWXUQ WKH QXPEHU 0RUH FRPSOH[ SUHIL[ H[SUHVVLRQV ZLOO LQYROYH RQH RU PRUH RSHUDWRUV 5HPHPEHU WKDW WKH RSHUDWRU JRHV LQ IURQW RI WKH RSHUDQGV LQ D SUHIL[ H[SUHVVLRQ 6R D VOLJKWO\ PRUH FRPSOH[ H[SUHVVLRQ ZRXOG EH WR KDYH WZR QXPEHUV DV RSHUDQGV ZLWK DQ RSHUDWRU LQ IURQW DV LQ + 2.6 3.7 7KLV H[SUHVVLRQ FRXOG LWVHOI EH DQ RSHUDQG LQ D ODUJHU H[SUHVVLRQ )RU H[DPSOH ZH PLJKW DVN IRU * + 2.6 3.7 + 5.2 18.7 $W WKH RXWHUPRVW OHYHO ZH KDYH D PXOWLSOLFDWLRQ RSHUDWRU ZLWK WZR RSHUDQGV * | operator + 2.6 3.7 \-------/ operand #1 + 5.2 18.7 \--------/ operand #2 ,Q RWKHU ZRUGV WKLV H[SUHVVLRQ LV FRPSXWLQJ WKH SURGXFW RI WZR VXPV +HUH LV WKH VDPH H[SUHVVLRQ LQ WKH PRUH IDPLOLDU LQIL[ QRWDWLRQ (2.6 + 3.7) * (5.2 + 18.7) 640 7KHVH H[SUHVVLRQV FDQ EHFRPH DUELWUDULO\ FRPSOH[ %XW WKH NH\ REVHUYDWLRQ WR PDNH DERXW WKH H[SUHVVLRQV WKDW LQYROYH RSHUDWRUV LV WKDW WKH\ DOO EHJLQ ZLWK DQ RSHUDWRU ,Q RWKHU ZRUGV HYHU\ SUHIL[ H[SUHVVLRQ LV RI RQH RI WZR IRUPV D VLPSOH QXPEHU DQ RSHUDWRU IROORZHG E\ WZR RSHUDQGV 7KLV REVHUYDWLRQ ZLOO EHFRPH D URDGPDS IRU RXU UHFXUVLYH VROXWLRQ 7KH VLPSOHVW SUHIL[ H[SUHVVLRQ ZLOO EH D QXPEHU DQG ZH FDQ GLVWLQJXLVK LW IURP WKH RWKHU FDVH EHFDXVH WKH RWKHU H[SUHVVLRQV EHJLQ ZLWK DQ RSHUDWRU 6R ZH FDQ EHJLQ RXU UHFXUVLYH VROXWLRQ E\ ORRNLQJ WR VHH LI WKH QH[W WRNHQ LQ WKH 6FDQQHU LV D QXPEHU ,I VR WKHQ ZH KDYH D VLPSOH FDVH DQG ZH FDQ VLPSO\ UHDG DQG UHWXUQ WKH QXPEHU public static double evaluate(Scanner input) { if (input.hasNextDouble()) { // base case with a simple number return input.nextDouble(); } else { // recursive case with an operator and two operands ... } } 7XUQLQJ RXU DWWHQWLRQ WR WKH UHFXUVLYH FDVH ZH NQRZ WKDW WKH LQSXW PXVW EH FRPSRVHG RI DQ RSHUDWRU IROORZHG E\ WZR RSHUDQGV :H FDQ EHJLQ E\ UHDGLQJ WKH RSHUDWRU public static double evaluate(Scanner input) { if (input.hasNextDouble()) { // base case with a simple number return input.nextDouble(); } else { // recursive case with an operator and two operands String operator = input.next(); ... } } $W WKLV SRLQW ZH UHDFK D FULWLFDO GHFLVLRQ :H KDYH UHDG LQ WKH RSHUDWRU 1RZ ZH QHHG WR VRPHKRZ UHDG LQ WKH ILUVW RSHUDQG DQG DIWHU WKDW ZH KDYH WR UHDG LQ WKH VHFRQG RSHUDQG ,I ZH NQHZ WKDW WKH RSHUDQGV ZHUH VLPSOH QXPEHUV ZH FRXOG VD\ public static double evaluate(Scanner input) { if (input.hasNextDouble()) { // base case with a simple number return input.nextDouble(); } else { // recursive case with an operator and two operands String operator = input.next(); double operand1 = input.nextDouble(); double operand2 = input.nextDouble(); ... } } 641 %XW ZH KDYH QR JXDUDQWHH WKDW WKH RSHUDQGV DUH VLPSOH QXPEHUV 7KH\ PLJKW EH FRPSOH[ H[SUHVVLRQV WKDW EHJLQ ZLWK RSHUDWRUV <RXU LQVWLQFW PLJKW EH WR WHVW ZKHWKHU RU QRW WKH RULJLQDO RSHUDWRU LV IROORZHG E\ DQRWKHU RSHUDWRU LQ RWKHU ZRUGV ZKHWKHU WKH ILUVW RSHUDQG EHJLQV ZLWK DQ RSHUDWRU EXW WKDW UHDVRQLQJ ZRQ W OHDG \RX WR D VDWLVIDFWRU\ RXWFRPH 5HPHPEHU WKDW WKH H[SUHVVLRQV FDQ EH DUELWUDULO\ FRPSOH[ 6R WKHUH PLJKW EH GR]HQV RI RSHUDWRUV WR EH SURFHVVHG LQ HLWKHU RI WKH RSHUDQGV 7KH VROXWLRQ WR WKLV SX]]OH LQYROYHV UHFXUVLRQ :H KDYH WZR RSHUDQGV WKDW ZH QHHG WR UHDG IURP WKH 6FDQQHU DQG WKH\ PLJKW EH YHU\ FRPSOH[ %XW ZH NQRZ WKDW WKH\ DUH LQ SUHIL[ IRUP DQG ZH NQRZ WKDW WKH\ DUHQ W DV FRPSOH[ DV WKH RULJLQDO H[SUHVVLRQ ZH ZHUH DVNHG WR HYDOXDWH 7KH NH\ LV WR UHFXUVLYHO\ HYDOXDWH HDFK RI WKH WZR RSHUDQGV public static double evaluate(Scanner input) { if (input.hasNextDouble()) { // base case with a simple number return input.nextDouble(); } else { // recursive case with an operator and two operands String operator = input.next(); double operand1 = evaluate(input); double operand2 = evaluate(input); ... } } 7KH VROXWLRQ LV VR VLPSOH WKDW LW DOPRVW GRHVQ W VHHP IDLU WR VROYH WKH SUREOHP VR HDVLO\ %XW WKLV DFWXDOO\ ZRUNV 2I FRXUVH ZH VWLOO KDYH WKH WDVN RI HYDOXDWLQJ WKH RSHUDWRU 6R DIWHU WKH WZR UHFXUVLYH FDOOV ZH ZLOO KDYH DQ RSHUDWRU DQG WZR QXPEHUV VD\ DQG DQG ,W ZRXOG EH QLFH LI ZH FRXOG MXVW VD\ return operand1 operator operand2; 8QIRUWXQDWHO\ -DYD GRHVQ W ZRUN WKDW ZD\ :H KDYH WR XVH D QHVWHG LI HOVH VWDWHPHQW WR WHVW ZKDW NLQG RI RSHUDWRU ZH KDYH DQG WR UHWXUQ DQ DSSURSULDWH YDOXH DV LQ if (operator.equals("+")) { return operand1 + operand2; } else if (operator.equals("-")) { return operand1 - operand2; } else if (operator.equals("*")) { ... 7KLV FRGH FDQ EH LQFOXGHG LQ LWV RZQ PHWKRG VR WKDW RXU UHFXUVLYH PHWKRG FDQ VWD\ IDLUO\ VKRUW 642 public static double evaluate(Scanner input) { if (input.hasNextDouble()) { // base case with a simple number return input.nextDouble(); } else { // recursive case with an operator and two operands String operator = input.next(); double operand1 = evaluate(input); double operand2 = evaluate(input); return evaluate(operator, operand1, operand2); } } &RPSOHWH 3URJUDP :KHQ \RX SURJUDP ZLWK UHFXUVLRQ \RX OO QRWLFH WZR WKLQJV )LUVW WKH UHFXUVLYH FRGH WKDW \RX ZULWH WHQGV WR EH IDLUO\ VKRUW HYHQ WKRXJK LW PLJKW EH VROYLQJ D YHU\ FRPSOH[ WDVN 6HFRQG \RX ZLOO JHQHUDOO\ ILQG WKDW PRVW RI \RXU SURJUDP HQGV XS EHLQJ VXSSRUWLQJ FRGH IRU WKH UHFXUVLRQ WKDW GRHV ORZ OHYHO WDVNV )RU WKLV SUREOHP ZH KDYH D VKRUW DQG SRZHUIXO SUHIL[ HYDOXDWRU EXW ZH QHHG WR LQFOXGH VRPH VXSSRUWLQJ FRGH WKDW H[SODLQV WKH SURJUDP WR WKH XVHU WKDW SURPSWV IRU D SUHIL[ H[SUHVVLRQ DQG WKDW UHSRUWV WKH UHVXOW :H DOVR IRXQG WKDW ZH QHHGHG D PHWKRG WKDW ZRXOG HYDOXDWH DQ RSHUDWRU DQG WZR RSHUDQGV 7KH QRQUHFXUVLYH SDUWV RI WKH SURJUDP DUH IDLUO\ VWUDLJKWIRUZDUG VR WKH\ DUH LQFOXGHG EHORZ ZLWKRXW GHWDLOHG GLVFXVVLRQ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.*; public class PrefixEvaluator { public static void main(String args) { Scanner console = new Scanner(System.in); System.out.println("This program evaluates prefix expressions"); System.out.println("for operators +, -, *, / and %"); System.out.print("expression? "); System.out.println("value = " + evaluate(console)); } // pre : input contains a legal prefix expression // post: expression is consumed and the result is returned public static double evaluate(Scanner input) { if (input.hasNextDouble()) { return input.nextDouble(); } else { String operator = input.next(); double operand1 = evaluate(input); double operand2 = evaluate(input); return evaluate(operator, operand1, operand2); } } // pre : operator is one of +, -, *, / or % // post: returns the result of applying the given operator to // the given operands public static double evaluate(String operator, double operand1, double operand2) { if (operator.equals("+")) { return operand1 + operand2; } else if (operator.equals("-")) { return operand1 - operand2; 643 34 35 36 37 38 39 40 41 42 43 44 } else if (operator.equals("*")) { return operand1 * operand2; } else if (operator.equals("/")) { return operand1 / operand2; } else if (operator.equals("%")) { return operand1 % operand2; } else { throw new RuntimeException("illegal operator " + operator); } } } 7KH SURJUDP FDQ KDQGOH VLPSOH QXPEHUV DV LQ WKH IROORZLQJ VDPSOH H[HFXWLRQ This program evaluates prefix expressions for operators +, -, *, / and % expression? 38.9 value = 38.9 ,W FDQ DOVR KDQGOH H[SUHVVLRQV ZLWK D VLQJOH RSHUDWRU DV LQ This program evaluates prefix expressions for operators +, -, *, / and % expression? + 2.6 3.7 value = 6.300000000000001 $QG LW KDQGOHV WKH FDVH ZH FRQVLGHUHG WKDW LQYROYHG D SURGXFW RI WZR VXPV This program evaluates prefix expressions for operators +, -, *, / and % expression? * + 2.6 3.7 + 5.2 18.7 value = 150.57000000000002 ,W FDQ KDQGOH DUELWUDULO\ FRPSOH[ H[SUHVVLRQV DV LQ WKH IROORZLQJ VDPSOH H[HFXWLRQ This program evaluates prefix expressions for operators +, -, *, / and % expression? / + * - 17.4 8.9 - 3.9 4.7 18.4 - 3.8 * 7.9 2.3 value = -0.8072372999304106 7KH H[SUHVVLRQ EHLQJ FRPSXWHG LQ WKLV H[DPSOH LV WKH SUHIL[ HTXLYDOHQW RI WKH IROORZLQJ LQIL[ H[SUHVVLRQ ((17.4 - 8.9) * (3.9 - 4.7) + 18.4) / (3.8 - 7.9 * 2.3) &KDSWHU 6XPPDU\ 5HFXUVLRQ LV DQ DOJRULWKPLF WHFKQLTXH ZKHUH D PHWKRG FDOOV LWVHOI $ PHWKRG WKDW XVHV UHFXUVLRQ LV FDOOHG D UHFXUVLYH PHWKRG 5HFXUVLYH PHWKRGV FRQWDLQ WZR FDVHV $ EDVH FDVH WKDW WKH PHWKRG FDQ VROYH GLUHFWO\ ZLWKRXW UHFXUVLRQ DQG D UHFXUVLYH FDVH ZKHUH WKH PHWKRG UHGXFHV D SUREOHP LQWR D VLPSOHU SUREOHP RI WKH VDPH NLQG ZLWK D UHFXUVLYH FDOO 644 5HFXUVLYH PHWKRG FDOOV ZRUN LQWHUQDOO\ E\ VWRULQJ LQIRUPDWLRQ DERXW HDFK FDOO LQWR D VWUXFWXUH FDOOHG D FDOO VWDFN :KHQ WKH PHWKRG FDOOV LWVHOI WKH LQIRUPDWLRQ DERXW WKLV FDOO DQG LWV SDUDPHWHU LQIRUPDWLRQ LV SODFHG RQ WRS RI WKH VWDFN :KHQ D PHWKRG FDOO ILQLVKHV H[HFXWLQJ LWV LQIRUPDWLRQ LV UHPRYHG IURP WKH WRS RI WKH VWDFN DQG WKH SURJUDP UHWXUQV WR WKH PHWKRG FDOO XQGHUQHDWK 5HFXUVLRQ LV XVHIXO IRU UHSUHVHQWLQJ PDQ\ FRPPRQ PDWKHPDWLFDO IXQFWLRQV VXFK DV H[SRQHQWLDWLRQ DQG ILQGLQJ WKH JUHDWHVW FRPPRQ GLYLVRU *&' RI WZR LQWHJHUV 5HFXUVLYH VROXWLRQV IRU PDWK RSHUDWLRQV RIWHQ PDWFK WKH PDWKHPDWLFDO GHILQLWLRQV IRU WKRVH RSHUDWLRQV PRUH FORVHO\ WKDQ DQ LWHUDWLYH VROXWLRQ $ UHFXUVLYH PHWKRG ZLWKRXW D EDVH FDVH RU RQH ZKHUH WKH UHFXUVLYH FDVH GRHVQ W SURSHUO\ WUDQVLWLRQ LQWR WKH EDVH FDVH FDQ OHDG WR LQILQLWH UHFXUVLRQ ,QILQLWH UHFXUVLRQ FUDVKHV \RXU SURJUDP EHFDXVH WKH PHWKRG FDOOV LWVHOI UHSHDWHGO\ XQWLO -DYD UXQV RXW RI FDOO VWDFN PHPRU\ 5HFXUVLRQ FDQ EH XVHG WR GUDZ JUDSKLFDO ILJXUHV LQ FRPSOH[ SDWWHUQV LQFOXGLQJ IUDFWDO LPDJHV )UDFWDOV DUH LPDJHV WKDW DUH UHFXUVLYHO\ FRQVWUXFWHG RU VHOI VLPLODU WKDW LV D VKDSH WKDW DSSHDUV VLPLODU DW DOO VFDOHV RI PDJQLILFDWLRQ DQG LV WKHUHIRUH RIWHQ UHIHUUHG WR DV LQILQLWHO\ FRPSOH[ 6HOI &KHFN 3UREOHPV 6HFWLRQ 7KLQNLQJ 5HFXUVLYHO\ :KDW LV UHFXUVLRQ" +RZ GRHV D UHFXUVLYH PHWKRG GLIIHU IURP D VWDQGDUG LWHUDWLYH PHWKRG" :KDW DUH EDVH FDVHV DQG UHFXUVLYH FDVHV" :K\ GRHV D UHFXUVLYH PHWKRG QHHG WR KDYH ERWK" &RQVLGHU WKH IROORZLQJ PHWKRG public static void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } } )RU HDFK FDOO EHORZ LQGLFDWH ZKDW RXWSXW LV SURGXFHG E\ WKH PHWKRG P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ &RQYHUW WKH IROORZLQJ LWHUDWLYH PHWKRG LQWR D UHFXUVLYH PHWKRG 645 // Writes n characters in a pattern, such as: // writeChars(1) prints * // writeChars(2) prints ** // writeChars(3) prints <*> // writeChars(4) prints <**> // writeChars(6) prints <<**>> // writeChars(12) prints <<<<<**>>>>> public static void writeChars(int n) { int half = (n - 1) / 2; for (int i = 0; i < half; i++) { System.out.print("<"); } if (n % 2 == 0) { System.out.print("**"); } else { System.out.print("*"); } for (int i = 0; i < half; i++) { System.out.print(">"); } } &RQYHUW WKH IROORZLQJ LWHUDWLYH PHWKRG LQWR D UHFXUVLYH PHWKRG // Prints each character of the string reversed twice. // stutterReverse("hello") prints oolllleehh public static void stutterReverse(String s) { for (int i = s.length() - 1; i >= 0; i--) { System.out.print(s.charAt(i)); System.out.print(s.charAt(i)); } } 6HFWLRQ $ %HWWHU ([DPSOH RI 5HFXUVLRQ :KDW LV D FDOO VWDFN DQG KRZ GRHV LW UHODWH WR UHFXUVLRQ" :KDW ZRXOG EH WKH HIIHFW LI WKH FRGH IRU WKH UHYHUVH PHWKRG ZHUH FKDQJHG WR WKH IROORZLQJ public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) String line = input.nextLine(); System.out.println(line); // swapped order reverse(input); // swapped order } } :KDW ZRXOG EH WKH HIIHFW LI WKH FRGH IRU WKH UHYHUVH PHWKRG ZHUH FKDQJHG WR WKH IROORZLQJ 646 public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) reverse(input); // moved this line String line = input.nextLine(); System.out.println(line); } } 6HFWLRQ 5HFXUVLYH )XQFWLRQV :KDW DUH WKH GLIIHUHQFHV EHWZHHQ WKH WZR YHUVLRQV RI WKH SRZ PHWKRG VKRZQ" :KDW DGYDQWDJH GRHV WKH VHFRQG YHUVLRQ KDYH RYHU WKH ILUVW YHUVLRQ" $UH ERWK YHUVLRQV UHFXUVLYH" &RQYHUW WKH IROORZLQJ LWHUDWLYH PHWKRG LQWR D UHFXUVLYH PHWKRG // Returns n!, such as 5! = 1*2*3*4*5 public static int factorial(int n) { int product = 1; for (int i = 1; i <= n; i++) { product *= i; } return product; } &RQVLGHU WKH IROORZLQJ PHWKRG public static int mystery1(int x, int y) { if (x < y) { return x; } else { return mystery1(x - y, y); } } )RU HDFK FDOO EHORZ LQGLFDWH ZKDW YDOXH LV UHWXUQHG P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ P\VWHU\ 7KH IROORZLQJ PHWKRG KDV D EXJ WKDW OHDGV WR LQILQLWH UHFXUVLRQ :KDW FRUUHFWLRQ IL[HV WKH FRGH" 647 // Adds the digits of the given number. // Example: digitSum(3456) returns 3+4+5+6 = 18 public static int digitSum(int n) { if (n > 10) { // base case (small number) return n; } else { // recursive case (large number) return n % 10 + digitSum(n / 10); } } 6HFWLRQ 5HFXUVLYH *UDSKLFV RSWLRQDO :KDW LV D IUDFWDO LPDJH" +RZ GRHV UHFXUVLYH SURJUDPPLQJ KHOS WR GUDZ IUDFWDOV" :ULWH -DYD FRGH WR FUHDWH DQG GUDZ D 3RO\JRQ LQ WKH VKDSH RI D UHJXODU KH[DJRQ ([HUFLVHV :ULWH D UHFXUVLYH PHWKRG QDPHG VWDU6WULQJ WKDW DFFHSWV DQ LQWHJHU DV D SDUDPHWHU DQG SULQWV D VWULQJ RI VWDUV DVWHULVNV AQ ORQJ L H WR WKH QWK SRZHU )RU H[DPSOH starString(0) starString(1) starString(2) starString(3) starString(4) should should should should should print print print print print * (because 2^0 == 1) ** (because 2^1 == 2) **** (because 2^2 == 4) ******** (because 2^3 == 8) **************** (because 2^4 == 16) 7KH PHWKRG VKRXOG WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ LI SDVVHG D YDOXH OHVV WKDQ :ULWH D PHWKRG QDPHG ZULWH1XPV WKDW WDNHV DQ LQWHJHU Q DV D SDUDPHWHU DQG WKDW SULQWV WKH ILUVW Q LQWHJHUV VWDUWLQJ ZLWK WR WKH FRQVROH LQ VHTXHQWLDO RUGHU DQG VHSDUDWHG E\ FRPPDV )RU H[DPSOH WKH IROORZLQJ FDOOV writeNums(5); System.out.println(); // to complete the line of output writeNums(12); System.out.println(); // to complete the line of output VKRXOG SURGXFH WKH RXWSXW 1, 2, 3, 4, 5 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 <RXU PHWKRG VKRXOG WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ LI SDVVHG D YDOXH OHVV WKDQ :ULWH D PHWKRG ZULWH6HTXHQFH WKDW DFFHSWV DQ LQWHJHU Q DV D SDUDPHWHU DQG WKDW SULQWV WR WKH FRQVROH D V\PPHWULF VHTXHQFH RI Q QXPEHUV ZLWK GHVFHQGLQJ LQWHJHUV HQGLQJ LQ IROORZHG E\ DVFHQGLQJ LQWHJHUV EHJLQQLQJ ZLWK 7KH WDEOH EHORZ LQGLFDWHV WKH RXWSXW WKDW VKRXOG EH SURGXFHG IRU YDULRXV YDOXHV RI Q 648 Method Call Output Produced ------------------------------------------writeSequence(1); 1 writeSequence(2); 1 1 writeSequence(3); 2 1 2 writeSequence(4); 2 1 1 2 writeSequence(5); 3 2 1 2 3 writeSequence(6); 3 2 1 1 2 3 writeSequence(7); 4 3 2 1 2 3 4 writeSequence(8); 4 3 2 1 1 2 3 4 writeSequence(9); 5 4 3 2 1 2 3 4 5 writeSequence(10); 5 4 3 2 1 1 2 3 4 5 1RWLFH WKDW IRU RGG QXPEHUV WKH VHTXHQFH KDV D VLQJOH LQ WKH PLGGOH ZKLOH IRU HYHQ YDOXHV LW KDV WZR V LQ WKH PLGGOH <RXU PHWKRG VKRXOG WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ LI SDVVHG D YDOXH OHVV WKDQ :ULWH D UHFXUVLYH PHWKRG QDPHG VWXWWHU WKDW DFFHSWV DQ LQWHJHU DV D SDUDPHWHU DQG UHWXUQV WKH LQWHJHU REWDLQHG E\ UHSODFLQJ HYHU\ GLJLW RI Q ZLWK WZR RI WKDW GLJLW )RU H[DPSOH VWXWWHU UHWXUQV 7KH FDOO VWXWWHU UHWXUQV &DOOLQJ VWXWWHU RQ D QHJDWLYH QXPEHU UHWXUQV WKH QHJDWLRQ RI FDOOLQJ VWXWWHU RQ WKH FRUUHVSRQGLQJ SRVLWLYH QXPEHU IRU H[DPSOH VWXWWHU UHWXUQV :ULWH D UHFXUVLYH PHWKRG QDPHG ZULWH%LQDU\ WKDW DFFHSWV DQ LQWHJHU DV D SDUDPHWHU DQG ZULWHV LWV ELQDU\ UHSUHVHQWDWLRQ WR WKH FRQVROH )RU H[DPSOH ZULWH%LQDU\ SULQWV :ULWH D UHFXUVLYH PHWKRG QDPHG SHUPXW WKDW DFFHSWV WZR LQWHJHUV Q DQG U DV SDUDPHWHUV DQG UHWXUQV WKH QXPEHU RI XQLTXH SHUPXWDWLRQV RI U LWHPV IURP D JURXS RI Q LWHPV )RU JLYHQ YDOXHV RI Q DQG U WKLV YDOXH 3 Q U FDQ EH FRPSXWHG DV IROORZV )RU H[DPSOH SHUPXW UHWXUQV RU VKRXOG UHWXUQ ,W PD\ EH KHOSIXO WR QRWH WKDW SHUPXW 7KH 6LHUSLQVNL FDUSHW LV D IUDFWDO GHILQHG DV IROORZV 7KH FRQVWUXFWLRQ RI WKH 6LHUSLQVNL FDUSHW EHJLQV ZLWK D VTXDUH 7KH VTXDUH LV FXW LQWR FRQJUXHQW VXEVTXDUHV LQ D E\ JULG ZLWK WKH FHQWUDO VXEVTXDUH UHPRYHG 7KH VDPH SURFHVV LV WKHQ DSSOLHG UHFXUVLYHO\ WR WKH RWKHU VXEVTXDUHV 7KH IROORZLQJ GLDJUDP VKRZV WKH ILUVW IHZ LWHUDWLRQV RI WKH FDUSHW 649 :ULWH D SURJUDP WR GUDZ WKH FDUSHW RQ D 'UDZLQJ3DQHO UHFXUVLYHO\ 7KH &DQWRU VHW LV D IUDFWDO GHILQHG E\ UHSHDWHGO\ UHPRYLQJ WKH PLGGOH WKLUGV RI OLQH VHJPHQWV :ULWH D SURJUDP WR GUDZ WKH &DQWRU VHW RQ D 'UDZLQJ3DQHO UHFXUVLYHO\ 3URJUDPPLQJ 3URMHFWV :ULWH D UHFXUVLYH SURJUDP WR VROYH WKH 0LVVLRQDULHV DQG &DQQLEDOV SUREOHP 7KUHH PLVVLRQDULHV DQG WKUHH FDQQLEDOV FRPH WR D ULYHU DQG ILQG D ERDW WKDW KROGV WZR ,I WKH FDQQLEDOV HYHU RXWQXPEHU WKH PLVVLRQDULHV RQ HLWKHU EDQN WKH PLVVLRQDULHV ZLOO EH HDWHQ +RZ VKDOO WKH\ FURVV" <RXU RXWSXW VKRXOG LQFOXGH WKH LQLWLDO SUREOHP WKH PRYHV \RX PDNH DQG D SLFWXUH RI WKH FXUUHQW VWDWH RI WKH SX]]OH DIWHU HDFK PRYH <RXU ILQDO RXWSXW VKRXOG SURGXFH RQO\ WKH PRYHV DQG SLFWXUH IRU WKRVH VWDWHV WKDW DUH RQ WKH VROXWLRQ SDWK :ULWH D UHFXUVLYH SURJUDP WR VROYH WKH 7RZHUV RI +DQRL SX]]OH 7KH SX]]OH LQYROYHV PDQLSXODWLQJ GLVNV DQG WKUHH GLIIHUHQW WRZHUV ZKHUH WKH GLVNV FDQ EH SODFHG <RX DUH JLYHQ D FHUWDLQ QXPEHU RI GLVNV LQ WKH H[DPSOH EHORZ RQ RQH RI WKH WKUHH WRZHUV 7KH GLVNV KDYH GHFUHDVLQJ GLDPHWHUV ZLWK WKH ODUJHVW GLVN RQ WKH ERWWRP | ******* *********** *************** ******************* A | | | | | B | | | | | C 650 7KH REMHFW RI WKH SX]]OH LV WR JHW DOO RI WKH GLVNV IURP RQH 7RZHU WR DQRWKHU VD\ IURP $ WR % 7KH WKLUG WRZHU LV SURYLGHG DV WHPSRUDU\ VWRUDJH VSDFH DV \RX PRYH GLVNV DURXQG <RX DUH RQO\ DOORZHG WR PRYH RQH GLVN DW D WLPH DQG \RX DUH QRW DOORZHG WR SODFH D GLVN RQ WRS RI D VPDOOHU RQH L H RQH ZLWK VPDOOHU GLDPHWHU ([DPLQH WKH UDWKHU VLPSOH VROXWLRQV IRU DQG GLVNV DQG VHH LI \RX FDQ W GLVFHUQ D SDWWHUQ 7KHQ ZULWH D SURJUDP WKDW ZLOO VROYH WKH 7RZHUV RI +DQRL SX]]OH IRU DQ\ QXPEHU RI GLVNV +LQW 0RYLQJ GLVNV LV D ORW OLNH PRYLQJ WKUHH GLVNV H[FHSW WKDW RQH DGGLWLRQDO GLVN LV RQ WRS :ULWH D UHFXUVLYH SURJUDP WR VROYH WKH (LJKW 4XHHQV SX]]OH 7KH SX]]OH LQYROYHV SODFLQJ HLJKW TXHHQV RQ D VWDQGDUG FKHVV ERDUG LQ VXFK D ZD\ WKDW QR WZR WKUHDWHQ HDFK RWKHU 7KH IROORZLQJ LV RQH H[DPSOH VROXWLRQ WR WKLV SUREOHP <RXU UHFXUVLYH SURJUDP VKRXOG H[SORUH DOO SRVVLEOH SODFHPHQWV RI HLJKW TXHHQV RQ WKH ERDUG UHSRUWLQJ DOO FRQILJXUDWLRQV ZKHUH QR WZR TXHHQV DUH LQ MHDSRUG\ 7KH .RFK VQRZIODNH LV D IUDFWDO FUHDWHG E\ VWDUWLQJ ZLWK D OLQH VHJPHQW WKHQ UHFXUVLYHO\ DOWHULQJ HDFK OLQH VHJPHQW DV IROORZV GLYLGH WKH OLQH VHJPHQW LQWR WKUHH VHJPHQWV RI HTXDO OHQJWK GUDZ DQ HTXLODWHUDO WULDQJOH WKDW KDV WKH PLGGOH VHJPHQW IURP VWHS DV LWV EDVH UHPRYH WKH OLQH VHJPHQW WKDW LV WKH EDVH RI WKH WULDQJOH IURP VWHS 7KH IROORZLQJ GLDJUDP VKRZV WKH ILUVW VHYHUDO LWHUDWLRQV RI WKH VQRZIODNH 651 :ULWH D SURJUDP WR GUDZ WKH .RFK VQRZIODNH RQ D 'UDZLQJ3DQHO UHFXUVLYHO\ 6WXDUW 5HJHV 0DUW\ 6WHSS 652 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online