C语言--汉诺塔程序执行步骤 C语言汉诺塔程序
C\u8bed\u8a00 \u6c49\u8bfa\u5854\u7a0b\u5e8f\u5f53n=3\u65f6\uff0c\u6267\u884c\u6b65\u9aa4\u600e\u4e48\u505a\uff1f\u7406\u89e3\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u53ef\u4ee5\u5148\u629b\u5f00\u9012\u5f52\u8fd9\u4ef6\u4e8b\uff0c\u5c31\u95ee\u9898\u672c\u8eab\u6765\u8ba8\u8bba\uff0c\u5148\u4e0d\u8981\u770b\u7a0b\u5e8f\u3002
\u4e09\u4e2a\u67f1\u5b50\u4e0a\uff0c\u5c0f\u7684\u5706\u76d8\u4e00\u5b9a\u5728\u5927\u7684\u4e0a\u9762\u3002\u628aA\u67f1\u4e0a\u7684\u76d8\u5b50n\u53f7\u76d8\u5b50\u79fb\u5230B\u67f1\u4e0a\uff0c\u5206\u6210\u4e24\u6b65\uff0c1\uff09\u628an\u4e4b\u524d\u7684\u79fb\u8d70\uff0c2\uff09\u628an\u53f7\u76d8\u79fb\u5230B\u67f1\u4e0a\uff0c3)\u628an\u4e4b\u524d\u7684\u76d8\u5b50\u79fb\u56de\u6765\u3002
\u5148\u628a\u8fd9\u4e2a\u95ee\u9898\u672c\u8eab\u641e\u6e05\u695a\uff0c\u518d\u6765\u8ba8\u8bba\u7a0b\u5e8f\u5b9e\u73b0\u3002
\u628an\u4e4b\u524d\u7684\u76d8\u5b50\u79fb\u8d70\u8fd9\u4e2a\u4e8b\uff0c\u4e0d\u662f\u7b80\u5355\u7684\u4e00\u6b21\u5c31\u53ef\u4ee5\u79fb\u8d70\u7684\uff0c\u8fd9\u662f\u4e00\u4e2a\u8fc7\u7a0b\u3002
\u8fd9\u4e2a\u8fc7\u7a0b\u8981\u501f\u52a9C\u67f1\uff0c
\u79fb\u52a8n-1\u4e2a\u76d8\u5b50\u7684\u8fc7\u7a0b\uff0c\u4e0e\u79fb\u52a8n\u4e2a\u76d8\u5b50\u7684\u8fc7\u7a0b\u76f8\u6bd4\uff0c\u9664\u4e86\u6570\u91cf\u5c11\u4e00\u4e2a\uff0c\u76ee\u6807\u662fA\u5230C\uff0c\u6ca1\u6709\u672c\u8d28\u7684\u4e0d\u540c\uff0c\u8fd9\u4e5f\u662f\u4f7f\u7528\u9012\u5f52\u7684\u57fa\u7840\u3002
\u628a\u89e3\u51b3\u95ee\u9898\u7684\u8fc7\u7a0b\u5f04\u660e\u767d\u4e86\uff0c\u518d\u6765\u770b\u7a0b\u5e8f\u5c31\u6bd4\u8f83\u5bb9\u6613\u4e86\u3002
n=3\uff0c\u79fb\u52a83\u4e2a\u76d8\u5b50
\u5b9e\u9645\u4e0a\u6211\u4eec\u5982\u679c\u624b\u5de5\u53bb\u505a\uff0c\u8981\u8fd9\u6837\uff0c
1# A-B
2# A-C
1# B-C
3# A-B\uff0c\u8fd9\u65f63#\u5df2\u7ecf\u5c31\u4f4d\u3002
1# C-A
2# C-B
1# A-B
\u8fd9\u662f\u79fb\u52a83\u4e2a\u76d8\u5b50\uff0c\u4eceA-B\u3002
\u8981\u79fb\u52a8\u7b2c4\u4e2a\u76d8\u5b50\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u505a\u4e86 4# A-C\uff0c\u7136\u540e\u91cd\u590d\u524d\u9762\u7684\u8fc7\u7a0b\uff0c\u628a3\u4e2a\u76d8\u5b50\u79fb\u52a8\u5230\u8fc7\u6765\u3002
\u4e0d\u8fc7\u521a\u624d\u79fb\u52a8\u76843\u4e2a\u76d8\u5b50\u662fA-B\uff0c\u73b0\u5728\u5219\u662fB-C\uff0c\u4f46\u57fa\u672c\u7684\u8fc7\u7a0b\u662f\u4e00\u6837\u7684\u3002
\u5177\u4f53 \u7684\u7a0b\u5e8f\u770b\u767e\u79d1\u770b\u5427\u3002
\u5c06\u4ee5\u4e0b\u5185\u5bb9\u5168\u90e8\u590d\u5236\u5230\u65b0\u5efa\u7684\u6e90\u6587\u4ef6\u4e2d\uff1a\uff08\u672c\u4eba\u81ea\u5df1\u5199\u7684\uff0c\u56e0\u4e3a\u4f60\u90a3\u8bfe\u672c\u4e0a\u7684\u4ee3\u7801\uff0c\u6ca1\u89e3\u91ca\uff0c\u4e66\u5199\u4e0d\u89c4\u8303\uff0c\u5f88\u96be\u7406\u89e3\u6e05\u695a\uff0c\u6240\u4ee5\u6211\u76f4\u63a5\u65b0\u5199\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u9644\u5e26\u8be6\u7ec6\u8bf4\u660e\uff09
#include
//\u6c49\u8bfa\u5854x\u5c42\u5854\u4eceA\u5854\u6574\u4f53\u642c\u5230C\u5854\uff0c\u4e2d\u95f4\u4e34\u65f6B\u5854\u3002
//x\u5c42\u5854\u662f\u4ece\u5927\u5230\u5c0f\u5f80\u4e0a\u53e0\u653e\u3002\u6bcf\u6b21\u79fb\u52a8\u53ea\u80fd\u79fb\u52a8\u4e00\u5c42\u5854\u3002\u5e76\u4e14\u5728\u79fb\u52a8\u8fc7\u7a0b\u4e2d\u5fc5\u987b\u4fdd\u8bc1\u5c0f\u5c42\u5728\u4e0a\u8fb9
//\u501f\u52a9B\u5854\u53ef\u4ee5\u5c06x\u5c42\u5854\u5168\u90e8\u4eceA\u642c\u5230C\u4e0a\uff0c\u5e76\u4e14\u7b26\u5408\u8981\u6c42\uff08\u5728\u79fb\u52a8\u8fc7\u7a0b\u4e2d\u5927\u7684\u90a3\u5757\u5728\u4e0b\u8fb9\uff0c\u5c0f\u7684\u90a3\u5757\u5728\u4e0a\u8fb9\uff09
int main()
{
void tower(int x,char a,char b,char c);//\u58f0\u660e\u51fd\u6570
int x=5,a='A',b='B',c='C';//x\u8868\u793a\u67095\u5c42\u5854\uff0c\u5177\u4f53\u8981\u591a\u5c11\u5c42\u81ea\u5df1\u4fee\u6539\u8fd9\u4e2a\u503c\u3002abc\u5206\u522b\u8868\u793aABC\u5854\u3002
tower(x,a,b,c);//x\u5c42\u5854\u4ecea\u79fb\u52a8\u5230c\u7684\u5168\u8fc7\u7a0b\uff0c\u4e3b\u7a0b\u5e8f\u53ea\u6709\u8fd9\u6761\u6709\u6548\u8bed\u53e5
return 0;
}
//\u4ee5\u4e0b\u662ftower\u51fd\u6570\u7684\u5b9a\u4e49
//\u53c2\u6570\u89e3\u6790\uff1ax\u5c42\u5854\u653e\u5728a\u4e0a\uff0cb\u662f\u4e2d\u95f4\u5854\uff0cc\u662f\u76ee\u6807\u5854\u3002\u5373x\u5c42\u5854\u8981\u4ecea\u642c\u5230c\u4e0a\u3002
//\u6b64\u51fd\u6570\u5b9e\u73b0x\u5c42\u5854\u4ecea\u6574\u4f53\u8f6c\u79fb\u5230c\u4e0a\u3002\u4ee5\u53ca\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u600e\u4e48\u642c\u7684\u5168\u90e8\u8fc7\u7a0b\u3002
void tower(int x,char a,char b,char c)
{
if(x==1)printf("\u5c06%d\u4ece%c\u653e\u5230%c\n",x,a,c);//\u53ea\u67091\u5c42\u5854\u65f6\uff0c\u76f4\u63a5\u4ecea\u642c\u5230c\u4e0a\u3002
else //\u4e0d\u6b621\u5c42\u5854\uff0c\u5219\u5148\u5c06x-1\u5c42\u5854\u4ecea\u6309\u7167\u89c4\u5f8b\u642c\u5230b\u4e0a\uff0c\u518d\u5c06\u6700\u540e\u4e00\u5757\u4ecea\u642c\u5230c\u4e0a\uff0c\u6700\u540e\u518d\u5c06b\u4e0a\u7684x-1\u5c42\u5854\u6309\u7167\u89c4\u5f8b\u642c\u5230c\u4e0a\u3002
{
tower(x-1,a,c,b);//\u5148\u5c06x-1\u5c42\u5854\u4ecea\u6309\u7167\u89c4\u5f8b\u642c\u5230b\u4e0a\uff0c\u6ce8\u610f\u53c2\u6570b\u653e\u5728\u6700\u540e\uff0c\u56e0\u4e3a\u653e\u5728\u6700\u540e\u7684\u53c2\u6570\u662f\u51c6\u5907\u642c\u8fc7\u53bb\u7684\u76ee\u6807\u5854\u3002
printf("\u5c06%d\u4ece%c\u653e\u5230%c\n",x,a,c);//\u5c06\u6700\u540e\u4e00\u5757\u4ecea\u642c\u5230c\u4e0a
tower(x-1,b,a,c);//\u6700\u540e\u518d\u5c06b\u4e0a\u7684x-1\u5c42\u5854\u6309\u7167\u89c4\u5f8b\u642c\u5230c\u4e0a\uff0c\u6ce8\u610f\u53c2\u6570b\u653e\u5728\u5f00\u5934\uff0c\u56e0\u4e3ax-1\u5c42\u662f\u8981\u4eceb\u4e0a\u642c\u8fc7\u53bb\u7684\u3002
}
}
hanoi(5, 'a', 'b', 'c');把5个从'a'移到'c'
这时n=5, noe='a', two='b', three='c'
因为n!=1, 执行else里的
hanoi( 4, 'a', 'c', 'b'); //把上面4个从a移到b
move( 'a', 'c'); //把第5个从a移到c
hanoi( 4, 'b', 'a', 'c'); //再把那4个从b移到c
上面的很好明白的, 再分析hanoi( 4, 'a', 'c', 'b'); //把上面4个从a移到b,也是执行else
hanoi( 3, 'a', 'b', 'c'); //把上面3个从a移到c
move( 'a', 'b'); //把第4个从a移到b
hanoi( 4, 'c', 'a', 'b'); //再把那3个从c移到b
一直到n=1才结束
http://zhidao.baidu.com/question/19996684.html
绛旓細2014-07-15 .缂栫▼瀹炵幇姹夎濉斾腑鐩樺瓙绉诲姩杩囩▼鐨勫姩鐢绘紨绀哄姛鑳姐(C璇█璁捐) 2012-08-22 C璇█姹夎濉闂,缂栫▼鎵撳嵃鐩樺瓙绉诲姩杩囩▼ 2016-05-12 姹夎濉旈棶棰樼殑C璇█绋嬪簭搴旇鎬庝箞鍐?骞惰璇存槑涓涓嬪師鍥 6 2016-06-01 C璇█ 姹夎濉旂▼搴褰搉=3鏃,鎵ц姝ラ鎬庝箞鍋? 2011-01-11 鐢–璇█缂栫▼搴忚В鍐虫眽璇哄闂 9 鏇村...
绛旓細杩欓噷浣犺鍏堝亣璁捐繖鏍风殑鍑芥暟鑳藉疄鐜版湰鍔熻兘锛墋 else { NuoYiWei(FromTa,ToTa);//灏卞墿涓涓鎸姩浜嗗氨鐩存帴鎸姩 } } 杩欎釜鍑芥暟鏄湪C++閲屽啓鐨勶紝濡傛灉鐢C璇█杩樿娉ㄦ剰浜涖傛垜杩欓噷杩樻湁鐢–鍐欑殑姹夎濉鐨绋嬪簭锛屼綘缁欐垜閭[email protected]鍙戦偖浠剁储瑕佸惂锛岃娓呮瑕佷簺浠涔堛
绛旓細include<stdio.h> /* Copyrighter by SS7E */ void hanoi(int n,char A,char B,char C) /* Copyrighter by SS7E */ { //褰撴寚鍓╀竴鍧楁槸锛屾妸A绉诲埌C if(n==1){ //杩欓噷鐨凙C涓嶆槸鎸嘇C锛岃屾槸鎸囧嚱鏁颁紶杩囨潵鐨勫弬鏁扮殑绗竴涓拰绗笁涓 //渚嬪杩欏彞 hanoi(n-1,A,C,B); 杩欐椂A鎸嘇锛孋...
绛旓細2銆佹妸闄ょ涓鐗囦互澶栵紝鍙互绉诲姩鐨勫彟澶栦竴鐗囩Щ鍔ㄥ埌鍙互绉诲姩鐨勪负姝紝杩欎釜鐪嬩技妯$硦锛屼絾鍏跺疄鍏崇郴鏄‘瀹氱殑锛岃繖涓椂鍊欏彧鏈変竴鐗囧彲浠ョЩ鍔紝鑰屼笖浣嶇疆涔熷彧鏈変竴涓彲浠ヨ瀹冪Щ鍔ㄣ傚氨鏄繖涔堜袱涓姝ラ锛岀敤鏉ュ畬鎴姹夎濉 涓嬮潰鏄垜鐨绋嬪簭 锛堟敞锛氭垜鍦ㄧ▼搴忎腑浣滀簡涓紛锛屽鏋滄槸濂囨暟鐗囷紝閭i『搴忎竴瀹氭槸A->C->B,锛堝叾涓瑼鏄師鏌憋紝B...
绛旓細杩欐牱鍐欐垜杩樻病鏈夎杩囷紝鎰熻涓嶆庝箞鏍凤紝鎴戣杩囦竴绉嶅啓娉曪紙浼唬鐮侊級鍋囪鍒濆鐘舵佷粠宸﹀埌鍙充緷娆℃槸鏌盉AC锛岀洏瀛愬湪A涓婏紙椤哄簭鍏跺疄鏃犳墍璋擄級锛岀洰鏍囦粠A鍒癈锛屽嵆鍙崇Щ N鏄洏瀛愪釜鏁扮洰锛+1琛ㄧず鍙崇Щ锛屽鏋滃埌浜嗗凡缁忓湪鏈鍙宠竟锛岀洿鎺ュ惊鐜埌鏈宸﹁竟鐨勶紱-1琛ㄧず涓庢姝eソ鐩稿弽 浼唬鐮 void hanoi锛坕nt N,int d锛//d=1 { ...
绛旓細鏈変汉棰勮█璇达紝杩欎欢浜嬪畬鎴愭椂瀹囧畽浼氬湪涓鐬棿闂數寮忔瘉鐏紝涔熸湁浜虹浉淇″﹩缃楅棬鑷充粖浠嶅湪涓鍒讳笉鍋滃湴鎼姩鐫鍦嗙洏銆**杈撳叆鏍煎紡瑕佹眰锛"%d" 鎻愮ず淇℃伅锛"Input the number of disks:"**杈撳嚭鏍煎紡瑕佹眰锛"Steps of moving %d disks from A to B by means of C:\n" "Move %d: from %c to %c\n"绋嬪簭杩愯...
绛旓細杩欐槸涓涓掑綊鐨勭畻娉曘傜涓姝ワ紝n-1涓噾鐗囦粠a缁c绉诲姩鍒癰 涓嶆槸鈥滀竴姝モ濆畬鎴愮殑锛岃屾槸鈥滀竴涓樁娈碘濓紙涓娆¢掑綊璋冪敤锛夊畬鎴愮殑銆傚湪鍋囧畾瀹冨畬鎴愮殑鍩虹涓婏紝绗簩姝ュ氨鍙互瀹屾垚浜嗐傚湪涓婇潰涓ゆ瀹屾垚鐨勫熀纭涓婏紝绗笁姝ワ紝n-1涓噾鐗囦粠b缁廰绉诲姩鍒癱锛屽畬鎴愬悗鍏ㄩ儴宸ヤ綔灏卞畬鎴愪簡銆=== 鑷充簬鈥渘-1涓噾鐗囦粠a缁廲绉诲姩...
绛旓細鍚岀悊涓や釜鐩樿繕鏄笉鍙互鐩存帴浠嶢绉诲姩鍒C鎵浠ヨ浠涓轰腑浠嬭冭檻灏1涓Щ鍔ㄥ埌B鐨杩囩▼銆傝繖娆℃槸浠涓轰腑浠嬶紝绉诲姩鍒癈涓虹洰鐨勭殑銆傛帴涓嬫潵鍐嶄竴娆¢掑綊璋冪敤move鍑芥暟锛堣涓哄洓锛夛紝灏辨槸绉诲姩鍒癇涓涓紝鍙互鐩存帴杩涜銆绋嬪簭鎵ц鈶 鈶e彞锛岀▼搴忚烦鍑烘渶鍐呬竴娆$殑璋冪敤锛堝嵆璺冲嚭绗洓娆$殑璋冪敤锛夎繑鍥炰笂涓娆★紙绗笁娆★級锛屽苟涓斾粠绗笁娆...
绛旓細n=1鏃讹紝灏辩洿鎺鎵ц褰㈠弬a鍒板舰鍙c(鎸夌収鍥剧墖鏍煎紡鎵撳嵃杈撳嚭瀛楃涓插氨琛屼簡鍟)銆傚叾瀹冩儏鍐垫寜鐓ч掑綊锛屽厛閫氳繃c鍏堟妸灏忕殑閮界Щ鍔ㄥ埌b锛屽啀鎶婃渶澶х殑绉诲姩鍒癱锛屾渶鍚庨氳繃a鎶奲鎵鏈夌洏绉诲姩鍒癱銆傛渶鍚庤緭鍑哄氨鏄綘鍥句腑鏁堟灉銆俰nclude<stdio.h> void doMove(int n,char x,char y);void move(int n,char a,char b,char...
绛旓細鎴戣繖鏈夎澶C鐨勫皬娓告垙銆傜粰浣犱竴涓熀纭鐨勭畝鍗曠殑姹夎濉旂▼搴銆備綘鐪嬬湅锛氳繖鏄釜姹夎濉旂▼搴忥紝鍦ㄨ皟璇曠殑鏃跺欙紝杈撳叆鐨勬暟瀛楁渶濂戒笉瑕佸ぇ浜15锛屽洜涓烘瘡澶т竴涓暟 鎵寰楃殑缁撴灉鐨姝ラ閮戒細澶氫竴鍊嶃傚鏋滀綘鏈夎愬績绛夊緟缁撴灉鐨勮瘽闄ゅ銆傛眽璇哄鏄湪娆ф床 娴佽鐨勪竴绉嶆父鎴忥紝鏈塧,b,c涓変釜绔裤俛绔夸笂鏈夎嫢骞蹭釜鐢卞ぇ鍒板皬鐨勫渾鐩橈紝澶х殑鍦...