Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tower of hanoi
//g++ 7.4.0 #include <bits/stdc++.h> using namespace std; int m=0; void TOH(int n, int a, int b, int c) { if( n==1) { cout<<"\n Move disk: "<<n<<" from "<<a <<" to "<<b<<endl; cout<<" Move disk: "<<n<<" from "<<b <<" to "<<c<<endl; m+=2; } else { TOH(n-1,a,b,c); cout<<"\n Move largest disk: "<<n<<" from disk "<<a <<" to "<<b<<endl; m++; TOH(n-1,c,b,a); cout<<"\nMove largest disk: "<<n<<" from "<<b <<" to "<<c<<endl; m++; TOH(n-1,a,b,c); } } int main() { int n,a=1,b=2,c=3; cin>>n; TOH(n,a,b,c); cout<<"Total moves:"<<m; return 0; }
run
|
edit
|
history
|
help
0
2015(M2)Simulare
Count squares
N Queens problem
Rectangle overlap
FInd rows with maximum no of 1's
xyp
informatika / Cinta Avrille X MIPA 5
reverse array
ad
Exempel 1