文章

37

粉丝

1

获赞

311

访问

5.8k

头像
Hanoi塔问题 题解:
P1082 复旦大学机试题
发布于2026年3月9日 12:28
阅读数 80

#include<bits/stdc++.h>
using namespace std;
int cnt = 0;
void Hanoi(char from, char to, char mid, int x){
	if(x==1){
		cout<<from<<"-->"<<to<<"   ";
		if(cnt == 4){
			cout<<endl;
		}
		
		cnt = (cnt+1)%5;
		
		return;
	}
	
	Hanoi(from,mid,to,x-1);
	
	Hanoi(from,to,mid,1);
	
	Hanoi(mid,to,from,x-1);
}


int main(){
	int n;
	while(cin>>n && n!=0){
		cnt = 0;
		Hanoi('A','C','B',n);
		cout<<endl;
	}
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发