¡@¥»½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¦ý¬On¨ú¥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(5) ¤@ª½³£¬O¥¼°õ¦æ§¹ªºª¬ºA¡A²{¦b Factorial_Recursive(4) ¤]°±¦b Return 4 * Factorial_Recursive(3) Åý°ïÅ|Åܦ¨¡G
[Factorial_Recursive: $n=3, ªð¦^¦ì¸m¦b Factorial_Recursive ³Ì«á¤@¦æ]¡@ª`·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(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¸gpºâ§¹¦¨µª®×¬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·Qn§äªºÈ»P°}¦C¤º¨ä¤¤¤@Ó¤¸¯À¤ñ¸û¡An¬O°}¦C¤¸¯À¸û¤j¡A¨º°}¦C¸Ì¨ä¥L¦ì¸m§ó«á±ªº¤¸¯À¤@©w¤]¤ñ·Q§äªºÈ¤j¡A¦]¦¹¥un©¹°}¦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¬On§äªº½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¤â¡Apºâ $low, $high ªº¥§¡ $mid, µM«á¬Ý¬Ý°}¦C¦b¤¤¶¡ $mid ¦ì¸mªºÈ»P $key ªºÃö«Y¡A¨M©wn§äþÓ¥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¡An¬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¤å³¹«Ê± |