Posted 灏忚┕瀛ython
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鍔犳槦鏍?锛屼笉杩疯矾 锛侌煈?/span>
鏈枃鎶曠鑷▼搴忓憳灏忓惔鍚屽 銆?br>
鍦ㄥ涔犮€屾暟鎹粨鏋勫拰绠楁硶銆嶇殑杩囩▼涓紝鍥犱负浜轰範鎯簡骞抽摵鐩村彊鐨勬€濈淮鏂瑰紡锛屾墍浠ャ€岄€掑綊銆嶄笌銆屽姩鎬佽鍒掋€嶈繖绉嶅甫寰幆姒傚康锛堢粫鏉ョ粫鍘伙級鐨勫線寰€鏄浉瀵规瘮杈冮毦浠ョ悊瑙g殑涓や釜鎶借薄鐭ヨ瘑鐐广€?/p>
绋嬪簭鍛樺皬鍚存墦绠椾娇鐢ㄥ姩鐢荤殑褰㈠紡鏉ュ府鍔╃悊瑙c€岄€掑綊銆嶏紝鐒跺悗閫氳繃銆岄€掑綊銆嶇殑姒傚康寤朵几鑷崇悊瑙c€屽姩鎬佽鍒掋€嶇畻娉曟€濇兂銆?/p>
浠€涔堟槸閫掑綊
鍏堜笅瀹氫箟锛?span class="mq-12">閫掑綊绠楁硶鏄竴绉嶇洿鎺ユ垨鑰呴棿鎺ヨ皟鐢ㄨ嚜韬嚱鏁版垨鑰呮柟娉曠殑绠楁硶銆?/strong>
閫氫織鏉ヨ锛岄€掑綊绠楁硶鐨勫疄璐ㄦ槸鎶婇棶棰樺垎瑙f垚瑙勬ā缂╁皬鐨勫悓绫婚棶棰樼殑瀛愰棶棰橈紝鐒跺悗閫掑綊璋冪敤鏂规硶鏉ヨ〃绀洪棶棰樼殑瑙c€傚畠鏈夊涓嬬壒鐐癸細
1. 涓€涓棶棰樼殑瑙e彲浠ュ垎瑙d负鍑犱釜瀛愰棶棰樼殑瑙?/span>
2. 杩欎釜闂涓庡垎瑙d箣鍚庣殑瀛愰棶棰橈紝闄や簡鏁版嵁瑙勬ā涓嶅悓锛屾眰瑙f€濊矾瀹屽叏涓€鏍?/span>
3. 瀛樺湪閫掑綊缁堟鏉′欢锛屽嵆蹇呴』鏈変竴涓槑纭殑閫掑綊缁撴潫鏉′欢锛岀О涔嬩负閫掑綊鍑哄彛
閫氳繃鍔ㄧ敾涓€涓竴涓壒鐐规潵杩涜鍒嗘瀽銆?/p>
1.涓€涓棶棰樼殑瑙e彲浠ュ垎瑙d负鍑犱釜瀛愰棶棰樼殑瑙?/span>
瀛愰棶棰樺氨鏄浉瀵逛笌鍏跺墠闈㈢殑闂鏁版嵁瑙勬ā鏇村皬鐨勯棶棰樸€?/p>
鍦ㄥ姩鍥句腑鈶犲彿闂锛堜竴鍧楀ぇ鍖哄煙锛夊垝鍒嗕负鈶″彿闂锛屸憽鍙烽棶棰樼敱涓や釜瀛愰棶棰橈紙涓ゅ潡涓尯鍩燂級缁勬垚銆?/p>
2. 杩欎釜闂涓庡垎瑙d箣鍚庣殑瀛愰棶棰橈紝闄や簡鏁版嵁瑙勬ā涓嶅悓锛屾眰瑙f€濊矾瀹屽叏涓€鏍?/span>
銆屸憼鍙峰垝鍒嗕负鈶″彿銆嶄笌銆屸憽鍙峰垝鍒嗕负鈶㈠彿銆嶇殑閫昏緫鏄竴鑷寸殑锛屾眰瑙f€濊矾鏄竴鏍风殑銆?/p>
3. 瀛樺湪閫掑綊缁堟鏉′欢锛屽嵆瀛樺湪閫掑綊鍑哄彛
鎶婇棶棰樺垎瑙d负瀛愰棶棰橈紝鎶婂瓙闂鍐嶅垎瑙d负瀛愬瓙闂锛屼竴灞備竴灞傚垎瑙d笅鍘伙紝涓嶈兘瀛樺湪鏃犻檺寰幆锛岃繖灏遍渶瑕佹湁缁堟鏉′欢銆?/p>
鈶犲彿鍒掑垎涓衡憽鍙凤紝鈶″彿鍒掑垎涓衡憿鍙凤紝鈶㈠彿鍒掑垎涓衡懀鍙凤紝鍒掑垎鍒扳懀鍙风殑鏃跺€欐瘡涓尯鍩熷彧鏈変竴涓笉鑳藉垝鍒嗙殑闂锛岃繖灏辫〃鏄庡瓨鍦ㄩ€掑綊缁堟鏉′欢銆?/p>
浠庨€掑綊鐨勭粡鍏哥ず渚嬪紑濮?/span>
涓€.鏁扮粍姹傚拰
1Sum(arr[0...n-1]) = arr[0] + Sum(arr[1...n-1])
鍚庨潰鐨?Sum 鍑芥暟瑕佽В鍐崇殑灏辨槸姣斿墠涓€涓?Sum 鏇村皬鐨勫悓涓€闂銆?/p>
1Sum(arr[1...n-1]) = arr[1] + Sum(arr[2...n-1])
浠ユ绫绘帹锛岀洿鍒板涓€涓┖鏁扮粍姹傚拰锛岀┖鏁扮粍鍜屼负 0 锛屾鏃跺彉鎴愪簡鏈€鍩烘湰鐨勯棶棰樸€?/p>
1Sum(arr[n-1...n-1] ) = arr[n-1] + Sum([])
浜?姹夎濉旈棶棰?/span>
姹夎濉旓紙Hanoi Tower锛夐棶棰樹篃鏄竴涓粡鍏哥殑閫掑綊闂锛岃闂鎻忚堪濡備笅锛?/p>
姹夎濉旈棶棰橈細鍙や唬鏈変竴涓⒌濉旓紝濉斿唴鏈変笁涓骇A銆丅銆丆锛孉搴т笂鏈?4涓洏瀛愶紝鐩樺瓙澶у皬涓嶇瓑锛屽ぇ鐨勫湪涓嬶紝灏忕殑鍦ㄤ笂銆傛湁涓€涓拰灏氭兂鎶婅繖涓洏瀛愪粠A搴хЩ鍒癇搴э紝浣嗘瘡娆″彧鑳藉厑璁哥Щ鍔ㄤ竴涓洏瀛愶紝骞朵笖鍦ㄧЩ鍔ㄨ繃绋嬩腑锛?涓骇涓婄殑鐩樺瓙濮嬬粓淇濇寔澶х洏鍦ㄤ笅锛屽皬鐩樺湪涓娿€?/p>
鈶? 濡傛灉鍙湁 1 涓洏瀛愶紝鍒欎笉闇€瑕佸埄鐢?B 濉旓紝鐩存帴灏嗙洏瀛愪粠 A 绉诲姩鍒?C 銆?/p>
鈶?濡傛灉鏈?2 涓洏瀛愶紝鍙互鍏堝皢鐩樺瓙 2 涓婄殑鐩樺瓙 1 绉诲姩鍒?B 锛涘皢鐩樺瓙 2 绉诲姩鍒?C 锛涘皢鐩樺瓙 1 绉诲姩鍒?C 銆傝繖璇存槑浜嗭細鍙互鍊熷姪 B 灏?2 涓洏瀛愪粠 A 绉诲姩鍒?C 锛屽綋鐒讹紝涔熷彲浠ュ€熷姪 C 灏?2 涓洏瀛愪粠 A 绉诲姩鍒?B 銆?/p>
鈶?濡傛灉鏈?3 涓洏瀛愶紝閭d箞鏍规嵁 2 涓洏瀛愮殑缁撹锛屽彲浠ュ€熷姪 C 灏嗙洏瀛?3 涓婄殑涓や釜鐩樺瓙浠?A 绉诲姩鍒?B 锛涘皢鐩樺瓙 3 浠?A 绉诲姩鍒?C 锛孉 鍙樻垚绌哄骇锛涘€熷姪 A 搴э紝灏?B 涓婄殑涓や釜鐩樺瓙绉诲姩鍒?C 銆?br>
鈶?浠ユ绫绘帹锛屼笂杩扮殑鎬濊矾鍙互涓€鐩存墿灞曞埌 n 涓洏瀛愮殑鎯呭喌锛屽皢灏嗚緝灏忕殑 n-1涓洏瀛愮湅鍋氫竴涓暣浣擄紝涔熷氨鏄垜浠姹傜殑瀛愰棶棰橈紝浠ュ€熷姪 B 濉斾负渚嬶紝鍙互鍊熷姪绌哄 B 灏嗙洏瀛怉涓婇潰鐨?n-1 涓洏瀛愪粠 A 绉诲姩鍒?B 锛涘皢A 鏈€澶х殑鐩樺瓙绉诲姩鍒?C 锛?A 鍙樻垚绌哄锛涘€熷姪绌哄 A 锛屽皢 B 濉斾笂鐨?n-2 涓洏瀛愮Щ鍔ㄥ埌 A锛屽皢 C 鏈€澶х殑鐩樺瓙绉诲姩鍒?C锛?B 鍙樻垚绌哄銆傘€傘€?/p>
涓?鐖彴闃堕棶棰?/span>
闂鎻忚堪锛?/p>
涓€涓汉鐖ゼ姊紝姣忔鍙兘鐖?涓垨2涓彴闃讹紝鍋囪鏈塶涓彴闃讹紝閭d箞杩欎釜浜烘湁澶氬皯绉嶄笉鍚岀殑鐖ゼ姊柟娉曪紵
鍏堜粠绠€鍗曠殑寮€濮嬶紝浠?4 涓彴闃朵负渚嬶紝鍙互閫氳繃姣忔鐖?1 涓彴闃剁埇瀹屾ゼ姊細
鍙互閫氳繃鍏堢埇 2 涓彴闃讹紝鍓╀笅鐨勬瘡娆$埇 1 涓彴闃剁埇瀹屾ゼ姊?/p>
鍦ㄨ繖閲岋紝鍙互鎬濊€冧竴涓嬶細鍙互鏍规嵁绗竴姝ョ殑璧版硶鎶婃墍鏈夎蛋娉曞垎涓轰袱绫伙細
鈶?绗竴绫绘槸绗竴姝ヨ蛋浜?1 涓彴闃?/span>
鈶?绗簩绫绘槸绗竴姝ヨ蛋浜?2 涓彴闃?/span>
鎵€浠?n 涓彴闃剁殑璧版硶灏辩瓑浜庡厛璧?1 闃跺悗锛宯-1 涓彴闃剁殑璧版硶 锛岀劧鍚庡姞涓婂厛璧?2 闃跺悗锛宯-2 涓彴闃剁殑璧版硶銆?/p>
鐢ㄥ叕寮忚〃绀哄氨鏄細
f(n) = f(n-1)+f(n-2)
鏈変簡閫掓帹鍏紡锛岄€掑綊浠g爜鍩烘湰涓婂氨瀹屾垚浜嗕竴鍗娿€傞偅涔堟帴涓嬫潵鑰冭檻閫掑綊缁堟鏉′欢銆?/p>
褰撴湁涓€涓彴闃舵椂锛屾垜浠笉闇€瑕佸啀缁х画閫掑綊锛屽氨鍙湁涓€绉嶈蛋娉曘€?/p>
鎵€浠?f(1)=1
銆?/p>
閫氳繃鐢?n = 2
锛?code class="mq-96">n = 3 杩欐牱姣旇緝灏忕殑鏁拌瘯楠屼竴涓嬪悗鍙戠幇杩欎釜閫掑綊缁堟鏉′欢杩樹笉瓒冲銆?/p>
n = 2
鏃讹紝f(2) = f(1) + f(0)
銆傚鏋滈€掑綊缁堟鏉′欢鍙湁涓€涓?code class="mq-100">f(1) = 1锛岄偅 f(2)
灏辨棤娉曟眰瑙o紝閫掑綊鏃犳硶缁撴潫銆?
鎵€浠ラ櫎浜?f(1) = 1
杩欎竴涓€掑綊缁堟鏉′欢澶栵紝杩樿鏈?f(0) = 1
锛岃〃绀鸿蛋 0 涓彴闃舵湁涓€绉嶈蛋娉曪紝浠庢€濈淮涓婁互鍙婂姩鍥句笂鏉ョ湅锛岃繖鏄惧緱鐨勬湁鐐逛笉绗﹀悎閫昏緫銆傛墍浠ヤ负浜嗕究浜庣悊瑙o紝鎶?f(2) = 2
浣滀负涓€绉嶇粓姝㈡潯浠讹紝琛ㄧず璧?2 涓彴闃讹紝鏈変袱绉嶈蛋娉曪紝涓€姝ヨ蛋瀹屾垨鑰呭垎涓ゆ鏉ヨ蛋銆?/p>
鎬荤粨濡備笅锛?/p>
鈶?鍋囪鍙湁涓€涓彴闃讹紝閭d箞鍙湁涓€绉嶈蛋娉曪紝閭e氨鏄埇 1 涓彴闃?/span>
鈶?鍋囪鏈変袱涓釜鍙伴樁锛岄偅涔堟湁涓ょ璧版硶锛屼竴姝ヨ蛋瀹屾垨鑰呭垎涓ゆ鏉ヨ蛋
閫氳繃閫掑綊鏉′欢锛?/p>
1f(1) = 1;
2f(2) = 2;
3f(n) = f(n-1)+f(n-2)
寰堝鏄撴帹瀵煎嚭閫掑綊浠g爜锛?/p>
1int f(int n) {
2 if (n == 1) return 1;
3 if (n == 2) return 2;
4 return f(n-1) + f(n-2);
5}
閫氳繃涓婅堪涓変釜绀轰緥锛屾€荤粨涓€涓嬪浣曞啓閫掑綊浠g爜锛?/p>
1.鎵惧埌濡備綍灏嗗ぇ闂鍒嗚В涓哄皬闂鐨勮寰?/span>
2.閫氳繃瑙勫緥鍐欏嚭閫掓帹鍏紡
3.閫氳繃閫掑綊鍏紡鐨勪复鐣岀偣鎺ㄦ暡鍑虹粓姝㈡潯浠?/span>
4.灏嗛€掓帹鍏紡鍜岀粓姝㈡潯浠剁炕璇戞垚浠g爜
浠€涔堟槸鍔ㄦ€佽鍒?/span>
浠嬬粛鍔ㄦ€佽鍒掍箣鍓嶅厛浠嬬粛涓€涓嬪垎娌荤瓥鐣ワ紙Divide and Conquer锛夈€?/p>
鍒嗘不绛栫暐
灏嗗師闂鍒嗚В涓鸿嫢骞蹭釜瑙勬ā杈冨皬浣嗙被浼间簬鍘熼棶棰樼殑瀛愰棶棰橈紙Divide锛夛紝銆岄€掑綊銆嶇殑姹傝В杩欎簺瀛愰棶棰橈紙Conquer锛夛紝鐒跺悗鍐嶅悎骞惰繖浜涘瓙闂鐨勮В鏉ュ缓绔嬪師闂鐨勮В銆?/p>
鍥犱负鍦ㄦ眰瑙eぇ闂鏃讹紝闇€瑕侀€掑綊鐨勬眰灏忛棶棰橈紝鍥犳涓€鑸敤銆岄€掑綊銆嶇殑鏂规硶瀹炵幇锛屽嵆鑷《鍚戜笅銆?/p>
鍔ㄦ€佽鍒掑叾瀹炲拰鍒嗘不绛栫暐鏄被浼肩殑锛屼篃鏄皢涓€涓師闂鍒嗚В涓鸿嫢骞蹭釜瑙勬ā杈冨皬鐨勫瓙闂锛岄€掑綊鐨勬眰瑙h繖浜涘瓙闂锛岀劧鍚庡悎骞跺瓙闂鐨勮В寰楀埌鍘熼棶棰樼殑瑙c€? 涓庛€屽垎娌荤瓥鐣ャ€嶃€屽姩鎬佽鍒掋€嶆蹇垫帴杩戠殑杩樻湁銆岃椽蹇冪畻娉曘€嶃€屽洖婧畻娉曘€嶏紝鐢变簬绡囧箙闄愬埗锛岀▼搴忓憳灏忓惔灏变笉鍦ㄨ繖杩涜灞曞紑锛屽湪鍚庣画鐨勬枃绔犱腑灏嗗垎鍒缁嗙殑浠嬬粛銆岃椽蹇冪畻娉曘€嶃€併€屽洖婧畻娉曘€嶃€併€屽垎娌荤畻娉曘€嶏紝鏁鍏虫敞锛氾級 灏嗐€屽姩鎬佽鍒掋€嶇殑姒傚康鍏抽敭鐐规娊绂诲嚭鏉ユ弿杩板氨鏄繖鏍风殑锛?/p>
1.鍔ㄦ€佽鍒掓硶璇曞浘鍙В鍐虫瘡涓瓙闂涓€娆?/span> 2.涓€鏃︽煇涓粰瀹氬瓙闂鐨勮В宸茬粡绠楀嚭锛屽垯灏嗗叾璁板繂鍖栧瓨鍌紝浠ヤ究涓嬫闇€瑕佸悓涓€涓瓙闂瑙d箣鏃剁洿鎺ユ煡琛ㄣ€? 杩樻槸浠?鐖彴闃?/strong> 涓轰緥锛屽鏋滀互閫掑綊鐨勬柟寮忚В鍐崇殑璇濓紝閭d箞杩欑鏂规硶鐨勬椂闂村鏉傚害涓篛(2^n)锛屽叿浣撶殑璁$畻鍙互鏌ョ湅绗旇€呬箣鍓嶇殑鏂囩珷 銆婂啺涓庣伀涔嬫瓕锛氭椂闂村鏉傚害涓庣┖闂村鏉傚害銆嬨€?/p>
鐩稿悓棰滆壊浠h〃鐫€ 鐖彴闃堕棶棰?鍦ㄩ€掑綊璁$畻杩囩▼涓噸澶嶈绠楃殑閮ㄥ垎銆?/p>
閫氳繃鍥剧墖鍙互鍙戠幇涓€涓幇璞★紝鎴戜滑鏄?鑷《鍚戜笅 鐨勮繘琛岄€掑綊杩愮畻锛屾瘮濡傦細 鎬濊€冧竴涓嬶細濡傛灉鍙嶈繃鏉ワ紝閲囧彇鑷簳鍚戜笂锛岀敤杩唬鐨勬柟寮忚繘琛屾帹瀵间細鎬庝箞鏍蜂簡锛?/p>
涓嬮潰閫氳繃琛ㄦ牸鏉ヨВ閲? 琛ㄦ牸鐨勭涓€琛屼唬琛ㄤ簡妤兼鍙伴樁鐨勬暟鐩紝绗簩琛屼唬琛ㄤ簡鑻ュ共鍙伴樁瀵瑰簲鐨勮蛋娉曟暟銆?br>鍏朵腑 绗竴娆¤凯浠o紝濡傛灉鍙伴樁鏁颁负 3 锛岄偅涔堣蛋娉曟暟涓?3 锛岄€氳繃 绗簩娆¤凯浠o紝濡傛灉鍙伴樁鏁颁负 4 锛岄偅涔堣蛋娉曟暟涓?5 锛岄€氳繃 鍔ㄦ€佽鍒掞紙Dynamic Programming锛?/span>
鍖哄埆鍦ㄤ簬杩欎簺瀛愰棶棰樹細鏈夐噸鍙狅紝涓€涓瓙闂鍦ㄦ眰瑙e悗锛屽彲鑳戒細鍐嶆姹傝В锛屼簬鏄垜浠兂鍒板皢杩欎簺瀛愰棶棰樼殑瑙e瓨鍌ㄨ捣鏉?/strong>锛屽綋涓嬫鍐嶆姹傝В杩欎釜瀛愰棶棰樻椂锛岀洿鎺ユ嬁杩囨潵灏辨槸銆?
鍏跺疄灏辨槸璇达紝鍔ㄦ€佽鍒掓墍瑙e喅鐨勯棶棰樻槸鍒嗘不绛栫暐鎵€瑙e喅闂鐨勪竴涓瓙闆嗭紝鍙槸杩欎釜瀛愰泦鏇撮€傚悎鐢ㄥ姩鎬佽鍒掓潵瑙e喅浠庤€屽緱鍒版洿灏忕殑杩愯鏃堕棿銆?
鍗崇敤鍔ㄦ€佽鍒掕兘瑙e喅鐨勯棶棰樺垎娌荤瓥鐣ヨ偗瀹氳兘瑙e喅锛屽彧鏄繍琛屾椂闂撮暱浜?/strong>銆傚洜姝わ紝鍒嗘不绛栫暐涓€鑸敤鏉ヨВ鍐冲瓙闂鐩镐簰瀵圭珛鐨勯棶棰橈紝绉颁负鏍囧噯鍒嗘不锛岃€屽姩鎬佽鍒掔敤鏉ヨВ鍐冲瓙闂閲嶅彔鐨勯棶棰樸€?/p>
浠庨€掑綊鍒板姩鎬佽鍒?/span>
f(n)
鏄?code class="mq-180">f(n-1)涓?code class="mq-181">f(n-2)鐩稿姞锛?code class="mq-182">f(n-1) 鏄?code class="mq-183">f(n-2)涓?code class="mq-184">f(n-3)鐩稿姞銆?/p>
f(n)
鑷簳鍚戜笂鐨勬眰瑙h繃绋嬨€?/p>
鍙伴樁鏁?/span>
1
2
3
4
5
6
7
8
9
璧版硶鏁?/span>
1
2
f(1) = 1
鍜?f(2) = 2
鏄墠闈㈡槑纭殑缁撴灉銆?/p>
f(3) = f(2) + f(1)
寰楁潵銆?/p>
鍙伴樁鏁?/span>
1
2
3
4
5
6
7
8
9
璧版硶鏁?/span>
1
2
3
f(4) = f(3) + f(2)
寰楁潵銆?/p>
鍙伴樁鏁?/span>
1
2
3
4
5
6
7
8
9
璧版硶鏁?/span>
1
2
3
5
鎴戠埍灏忚┕
/ 浠婃棩浜掑姩 /
鐣欒█涓婚 : 绠€鐭殑璇濊鏄庝粈涔堟槸閫掑綊鎴栬€呬粈涔堟槸鍔ㄦ€佽鍒掞紝涔熷氨鏄鏈枃鐨勬敹鑾?銆?/span>
鐣欒█鏍煎紡 鈥斺€?/span> Day xx : blablabla 锛涜繖閲屽己璋冧笅 锛?/span>涓嶇鍚堜富棰樺拰鏍煎紡鐨勬墦鍗℃棤鏁堝櫌 锛?/span>搴斾粯宸簨鐨勬€佸害鏄笉瀹规槗甯嚜宸卞吇鎴愪竴涓ソ涔犳儻鐨勫憿 锛屼互鍚庢墦鍗″け璐ョ殑灏变笉涓€涓釜鍥炲浜?锛岀Щ鍏ョ簿閫夌殑琛ㄧず绗﹀悎 锛屾彁鍓嶈娓呮姣旇緝濂?锛岄伩鍏嶅埌鏃跺€欏厬鎹㈠按灏?锛屾瘯绔熸墦鍗$洰鐨勪富瑕佹槸鍏绘垚涓€涓ソ涔犳儻 锛屽悓鏃朵篃甯屾湜鍚勪綅鑳借鐪熻鏂囩珷 锛屾墍浠ヤ富棰橀兘鏄枃绔犵浉鍏崇殑鍛?锛?/span>
鎺ㄨ崘闃呰锛?/span>
杩欎箞纭殑骞茶揣锛屼笉缁欎釜濂界湅锛燂紒
娆㈣繋鍏虫敞杩欎釜楠氬寘绋嬪簭鐚筐煈?/strong>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章