ºô­¶¼ÐÃD¡G 10 ¨Ï¥ÎªÌ¦Û©w¸q¨ç¦¡ (3): »¼°j

Warning: fopen(/home/crazy/www/cmsb/bcj/has_read.php): failed to open stream: Permission denied in /home/crazy/www/compose/reading.php on line 2077

Warning: fputs() expects parameter 1 to be resource, bool given in /home/crazy/www/compose/reading.php on line 2079

Warning: fclose() expects parameter 1 to be resource, bool given in /home/crazy/www/compose/reading.php on line 2080
¡@
¡T¡T¡TÆ[¬Ý¯d¨¥¡G¦¹¤å³¹¤w¸g¦³2«h¯d¨¥ ¡T¡T¡T


¡@¥»½g¥þ³¡¥Î¨Ó¤¶²Ð¡u»¼°j¨ç¦¡¡v¡]¦h¼g¬°¡u»¼°j¡v¡A¤]§@¡u»¼¦^¡v¡B¡u»¼Âk¡v¡^¡C»¼°j (recursive) ¨ç¦¡ªº¯S¼x¬O¨ç¦¡¡u©I¥s¦Û¤v¡v¡A¤S¤À¬°ª½±µ»¼°j»P¶¡±µ»¼°j¡C»¼°j¥i»¡¬O¸Ñ¨M°ÝÃDªº§Q¾¹¡A«D±`¦h¦³¦Wªººtºâªk¦p§Ö³t±Æ§Ç (quicksort), ¤G¤À·j´M (binary search), ¥H¤Î¾ð (tree), ¹Ï (graph) µ¥¸ê®Æµ²ºcªº¾Þ§@³£¥õ¿à¥¦¡C

¡@°£¥»¤å´£¨Ñªº½d¨Ò¤§¥~¡AŪªÌ¤]¥i¥H°Ñ¦Ò³o½g¤å³¹ªº²Ä¥|¬q¡C


¡@¥H¤U´N¥Î¶¥­¼ (factorial) ªº­pºâ¬°¨Ò¡A®i¥Ü¦p¦ó¼g¥X»¼°j¨ç¦¡¡G

¡@µù¡G¶¥­¼§Y³sÄò¾ã¼Æªº­¼¿n¡A¼Æ¾Ç¤W²Å¸¹¥Î "!", n! = n*(n-1)*(n-2)*...*2*1, ¦ý¬O 0! = 1! = 1.

¡@¤G­Óª©¥»³£ºâ¥X¤@¼Ëµª®× 120. °j°é°µªk«Üª½Ä±¦a±q 1 ¶}©l¡A­¼¥H 2 ¦A­¼¥H 3, ¤@ª½­¼¨ì $n. µM¦Ó»¼°jªº·Qªk¬O¡A$n ¶¥­¼´N¬O $n ­¼¥H $n-1 ¶¥­¼¡A©ó¬O $n-1 ¶¥­¼¥¿¦n¬°¦P¼Ë¡u¶¥­¼¡v³o­Ó¨ç¦¡¥N¤J $n-1 ªºµª®×¡A¦]¦¹¥i¥H©I¥s¦Û¤v¡C

¡@¬Û§Î¤§¤U¡A»¼°j¨ç¦¡¼gªk¤ñ°j°é¤Ö«Ü¦hµ{¦¡½X¡C»¼°j¨ç¦¡ªº¹ê§@¤À¬°¤G³¡¥÷¡AÃä¬É±ø¥ó»P»¼°jÃö«Y¦¡¡C¡u¶¥­¼¡vªº»¼°jÃö«Y¬° $n ¶¥­¼µ¥©ó $n ­¼¥H $n-1 ¶¥­¼¡A¦ý¦pªG¥ÃµL¤îºÉªº»¼°j¤U¥h¡Aµ¥©ó $n ­¼¥H $n-1 ­¼¥H $n-2 ¶¥­¼¡A¦A®i¶}¨ì $n-3 ¶¥­¼¡K¡K´N¸òµL½a°j°é¤@¼Ë¶]¤£§¹¡A¦]¦¹»Ý­nÃä¬É±ø¥ó¨Ï¨ä°±¤î»¼°j¡C³q±`Ãä¬É±ø¥ó¬O¤@¤p²Õ¤wª¾ªºµª®×¡A¥B¥ô¦ó¿é¤J $n ³£·|®i¶}¨ì¥¦¡A¦p¶¥­¼¿ï¾Ü 0 »P 1 ¶¥­¼³£¬O 1, 2 ¥H¤Wªº¶¥­¼³£·|¦]¬°¥N¤J $n-1 ³vº¥§â¿é¤JÅܤp¨ì 1 ¦Ó²×¤î»¼°j¡C

¡@¥H¤U¸Ô²Ó­åªR Factorial_Recursive(5) °õ¦æªº¹Lµ{¡C·í©I¥s¨ç¦¡¡A¤£¦ý¤Þ°_°õ¦æ¶¶§Ç§ïÅÜ¡AÁÙ·|§ïÅÜ°O¾ÐÅ餤ªº°ïÅ| (stack). ¡u°ïÅ|¡v¬O¤@­Ó·Q¹³ªº¸ê®Æµ²ºc¡A¦n¹³¥¬¤B±í¤@¼Ë¡A¥úºÐ±q¤U­±³vº¥Å|¤W¥h¡A¦ý¬O­n¨ú¥X¸ê®Æ®É¥u¯à¥ý²¾°£¤W­±ªº¥úºÐ¤~¯à®³¨ì¤U­±ªº¡A³o¯S©ÊºÙ¬°¡u«á«l¥ý¥X¡v¡C¥úºÐ´N¬O°ïÅ|¸Ìªº¨Cµ§°O¿ý (record), ¹J¨ì¨ç¦¡©I¥s´N·|´Â°ïÅ|±À¤J¤@­Ó·sªº°O¿ý¡A°O¿ý¸Ì«O¦³¨ç¦¡·í¤U¨Ï¥Îªº°Ñ¼Æ»P°Ï°ìÅܼơA¥H¤Î¨ç¦¡µ²§ô®É­nªð¦^ªº¦ì¸m¡CÃö©ó¡u°ïÅ|¡v§ó¸Ô²ÓªºÆ[©À¥i¬d¾\­pºâ¾÷·§½×¡B¸ê®Æµ²ºc¤Îµ{¦¡»y¨¥³]­pªº®ÑÄy¡C

¡@·í°õ¦æ¨ì Factorial_Recursive(5) ®É¡A°ïÅ|¸Ì·s¼W¤F²Ä¤@­Ó°O¿ý¡A¼gµÛ $n = 5 ¤Îªð¦^¦ì¸m¡A¦p¤U¡G

[Factorial_Recursive: $n=5, ªð¦^¦ì¸m¦b­Ë¼Æ²Ä¤G¦æ MsgBox]
¡@¥¦±N°õ¦æ¨ì Return 5 * Factorial_Recursive(4), Åý°ïÅ|Åܦ¨¡G

[Factorial_Recursive: $n=4, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=5, ªð¦^¦ì¸m¦b­Ë¼Æ²Ä¤G¦æ MsgBox]
¡@Factorial_Recursive(5) ¤@ª½³£¬O¥¼°õ¦æ§¹ªºª¬ºA¡A²{¦b Factorial_Recursive(4) ¤]°±¦b Return 4 * Factorial_Recursive(3) Åý°ïÅ|Åܦ¨¡G

[Factorial_Recursive: $n=3, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=4, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=5, ªð¦^¦ì¸m¦b­Ë¼Æ²Ä¤G¦æ MsgBox]
¡@ª`·N«á¨Ó´¡¤Jªº¨âµ§°O¿ý¡Aªð¦^¦ì¸m¤@¼Ë¡A¦]¬°³£¦b¨º­Ó¦a¤è»¼°j©I¥s¦Û¤vªº¡CÃþ±À¤U¥h¡A°ïÅ|³Ì²`®É¤º®e¬O¡G

[Factorial_Recursive: $n=1, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=2, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=3, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=4, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]
[Factorial_Recursive: $n=5, ªð¦^¦ì¸m¦b­Ë¼Æ²Ä¤G¦æ MsgBox]
¡@Factorial_Recursive(1) ¤£¦A»¼°j¡A°õ¦æ Return 1 ¦Óµ²§ô¡A°ïÅ|§R¥h³Ì¤W¼h¡A¨Ãªð¦^ Factorial_Recursive(2) ®É¥d¦íªº Return 2 * Factorial_Recursive(1) ³oÃä¡A«á­± Factorial_Recursive(1) ¤w¸g­pºâ§¹¦¨µª®×¬O 1, ©Ò¥HÅÜ Return 2 * 1 µM«á±N 2 ¦^¶Ç¨Ãµ²§ô°õ¦æ¡C

¡@Ãþ±À¤U¥h Factorial_Recursive(3) ±N Return 3 * 2, Factorial_Recursive(4) ±N Return 4 * 6, Factorial_Recursive(5) ±N Return 5 * 24, ©ó¬Oªð¦^ MsgBox ¨º¦æ®Éµª®×´N¬O 120.

¡@»¼°j¨ç¦¡°õ¦æªº¹Lµ{¤@©w¦ñÀH¤j¶qÀWÁc°ïÅ|ªº®øªø¡A¦]¦¹¬Û·í¯Ó¥Î°O¾ÐÅé¸ê·½¡A¦pªGµL½a»¼°j«h³Ì«á±N¦]°O¾ÐÅé¯ÓºÉ¦Óµ²§ô¡C°j°é¹ê§@¶¥­¼¬Ý°_¨ÓÁöµM¦h´X¦æµ{¦¡½X¡A¦ý°õ¦æ¹Lµ{²³æ¤S¤£¯Ó¥Î°O¾ÐÅé¡A¦]¦¹¤]¤£¬O©Ò¦³¨Æ±¡³£­n¾aµ{¦¡½X²³æªº»¼°j¸Ñ¨M¡A¦ý¬O¦³³\¦hºtºâªk»Ý­n°ïÅ|ªº°t¦X¡A¥H»¼°j¹ê§@ªº¦¨¥»»·§C©ó°j°é¡C

¡@­è¤~¥u®i¥Ü¡uª½±µ»¼°j¡v¡A¡u¶¡±µ»¼°j¡v´N¬O¦b¨ç¦¡°õ¦æ¹Lµ{¤¤©I¥s¤F§Oªº¨ç¦¡¤¤¦³©I¥s¦^¦Û¤v¡A§Y¨ç¦¡ A ©I¥s B, B ¤S©I¥s A. ¦]¬°¤ñ¸û¤Ö¥Î¡A¥»¤å¤£¦AÁ|¨Ò»¡©ú¡C


¡@²Ä¤G­Ó½d¨Ò®i¥Ü¨Ï¥Î»¼°j½s¼g¤G¤À·j´M¡C°²³]¤@­Ó°}¦C¤w³Q±Æ§Ç¦¨¥Ñ¤p¦Ó¤j¡A±N·Q­n§äªº­È»P°}¦C¤º¨ä¤¤¤@­Ó¤¸¯À¤ñ¸û¡A­n¬O°}¦C¤¸¯À¸û¤j¡A¨º°}¦C¸Ì¨ä¥L¦ì¸m§ó«á­±ªº¤¸¯À¤@©w¤]¤ñ·Q§äªº­È¤j¡A¦]¦¹¥u­n©¹°}¦Cªº¥t¤@­Ó¤è¦V§ä´N¥i¥H¡C¨C¦¸¤ñ¸û³£¥i¥H±Æ°£±¼½d³ò¤º¤@¥bªº¤¸¯À¡A¦]¦ÓºÙ¬°¡u¤G¤À¡v·j´M¡C

¡@³o­Ó½d¨Òªºµ²ºc¤ñ­è¤~½ÆÂø³\¦h¡A»¼°jÃö«Y¦¡¦³¨â±ø¡AÃä¬É±ø¥óªº¦ì¸m¤]¤£©úÅã¡C¤G¤À·j´M°±¤îªºª¬ªp¨ä¹êª½Ä±¤W¦³¤TºØ¡A§ä¨ìµª®×¡B¨S§ä¨ìµª®×¸ò©I¥sªÌ¬G·N¶Ãµ¹°Ñ¼Æ¡C

¡@¤G¤À·j´Mªº¥|­Ó°Ñ¼Æ¤À§O¬°­n§äªº­È¡AºÙ¬°¡uÁä¡v (key), ³Q§äªº°}¦C¡B³Q§ä½d³òªº¤U¬É¸ò¤W¬É¡A¤]´N¬O­n§äªº½d³ò¦b $low ¨ì $high, $low ³Ì¤p¥i¥H 0, $high ³Ì¤j¥i¥H UBound($array)-1, $high ªº³Ì¤j­È´N¬O°}¦C¸Ì³Ì«á¤@­Ó¤¸¯Àªº¯Á¤Þ¦ì¸m¡C

¡@¤@¶}©l§Ú­Ì¥Î¡uÀq»{°Ñ¼Æ¡vªº§Þ¥©¡AÅý©I¥sªÌ¥i¥H¤£¥²¿Ë¦Û«ü©w½d³ò¡A¦]¬°¤@¯ë¦Ó¨¥¡u·j´M¡v¬O¹ï©ó¾ã­Ó°}¦Cªº¡C¨ç¦¡­º¥ý¹LÂo Default §â¥¦§ï¦¨­è¤~´£¨ìªº $low ³Ì¤p¤Î $high ³Ì¤j¡A³o¬O¹w³]ªº·j´M½d³ò¡C

¡@½T©w¦n·j´M½d³ò«á¡A¥ý¬D¥X°Ñ¼Æ¤£¦X²zªºª¬ªp¡A´N¬O $low ¤ñ $high ¤j¡C³oùبϥγæ¦æ If...Then ¤À¤ä¡A¦]¬°²Å¦X±ø¥ó´N Return ¸õ¥X¨ç¦¡¡A¨ä¾l³¡¤À¥i¥H¤£¥² Else ¥]¦í¡C³oºØ¼gªk¥i¥Hµø¬°¡u¹LÂo¡v¤£¦X²zªºª¬ªp¨Ï¤§¤£Ä~Äò°õ¦æ¡C

¡@±µµÛ±q°}¦C¤¤¥¡¤U¤â¡A­pºâ $low, $high ªº¥­§¡ $mid, µM«á¬Ý¬Ý°}¦C¦b¤¤¶¡ $mid ¦ì¸mªº­È»P $key ªºÃö«Y¡A¨M©w­n§ä­þ­Ó¥bÃä©Î§ä¨ìµª®×¤F¡Cºâ¥­§¡®É­n¥h±¼¤p¼Æ 0.5 ªº³¡¥÷¡A©Ò¥H¥Î Floor ¤º«Ø¨ç¦¡¿ì¨ìµL±ø¥ó±Ë¥h¡C

¡@$key »P°}¦C $mid ¦ì¸mªº¤¸¯À¤ñ¸û¡A$key ¸û¤p§ä¥ª¥b¡A¸û¤j§ä¥k¥b¡A­n¬O¤@¼Ëªí¥Ü $mid ´N¬O§Ú­Ì­nªºµª®×¦ì¸m¡C§ä¥ª¥bªº®É­Ô¦]¬° $mid ¤w¸g½T©w¤£¬Oµª®×¡A©Ò¥H½d³ò¶ñ¤J $low ¨ì $mid-1; §ä¥k¥b¤]¦] $mid ¦ì¸m½T©w«Dµª®×©Ò¥H±q $mid+1 ¨ì $high.

¡@¦Ü¦¹¡A¤w¸g¦Ò¼{¤F»¼°jÃö«Y¦¡©M¨âºØÃä¬É±ø¥ó¡A¥i¬O¡u§ä¤£¨ì¡v³oºØª¬ªp·|¦p¦ó¡H´M§ä¹Lµ{¤¤ $low, $high ¤£Â_¾aªñ¡A³Ì«á±NÅÜ«° $low µ¥©ó $high, ¤]´N¬O³Q§äªº½d³ò³Ñ¤U¤@­Ó¤¸¯À¡A¦¹®É¤S¤£¬O¥¿½Tµª®×¦Ó¶Ç¦^ $mid, ¤@©w·|¦A¡u©¹¥ª¡v©Î¡u©¹¥k¡v§ä¡A©ó¬O³y¦¨¤£¦X²z¿é¤J¡A¦b Return -1 ³Q¹LÂo±¼¦Ó¾ã­Óµ²§ô¡C

¡@³o­Ó½d¨Ò»P 10-1 ¤£¦P³B¦b©ó¡A¥¦»¼°jªº¦¸¼Æ¤£¤@©w¡A¤£¹³½d¨Ò 10-1 ¿é¤J $n ¤@©w·|»¼°j $n ¦¸¡C¦³¿³½ìªºÅªªÌ¥i¥H±´¯Áºtºâªk (algorithm) ªº¬ÛÃö¤º®e¡A¤G¤À·j´Mªº³Ì®tª¬ªp¬O»¼°j log n ¦¸¡A³o­Ó n ¬O³Q§äªº°}¦Cªø«×¡A»P¸ê®Æ¤º®e©MÁäµLÃö¡Alog ¬O¼Æ¾Ç¤Wªº¡u¹ï¼Æ¡v¡C


¦^ ¡P §Úªº AutoIt ¾Ç²ßµ§°O ³o¤@½g¤å³¹«Ê­±


¥»¤å±i¶KªÌ¡GBo-Cheng Jhan¡e±i¶K®É¶¡¡G¥Á°ê105¦~5¤ë17¤é¡]¬P´Á¤G¡^18ÂI07¤À¡f

³¡¸¨®æ­º­¶


¾Ç²ßªº¬G¶m­º­¶
¥»¯¸¤½§i¡G¡e±z¶V»Ý­n§Ú­Ì¡A§Ú­Ì´N¶V¦³³Ð·N¡f ¥»¯¸»¡©ú®Ñ¡G¡eµo²{¬G¶mÁÙ¦³§ï¶iªº¦a¤è¡A½Ð¨Ó«H§i¶D­ì¤B­Ì¡f
Æ[¹îÀ³¥Î¾Ç²ßÂI¼Æ ¡G¡e«¥ªº¬G¶m¦³±zªº°Ñ»P¡A·|¨Ï§Ú­Ì¦³§ó¤jªºµo´§ªÅ¶¡¡A®i²{§óÂ×´Iºë±mªº¾Ç²ßµe­±¡f ¡e´Á«ÝÂǥѵL»Ùêºô­¶³]­p¡A¯àÅýµø»Ù¤pªB¤Í§ó·R¬Ý®Ñ¡B§ó·R¼g§@¥B§ó·R¾Ç²ß¡f¡Gª¼¥Î¹q¸£¡u¤ß±o¤À¨É¡v
¡e¬°¤FÅý§Ú­Ì¦³°®²bªº¾Ç²ßÀô¹Ò¡A½Ð¤Å¥ô·N¦b¥»¯¸´²¼½°Ó·~¼s§i»P¤£¦Xªk¤å¥ó©ÎÁpµ²¡f¡G¥»¯¸«Å¥Ü