-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrec4.java
37 lines (33 loc) · 858 Bytes
/
rec4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class rec4 {
public static void tower(int n,String src,String helper,String dest){
if (n==1){
System.out.println ("transfer "+n+" from "+src+" to "+dest);
return;
}
tower(n-1,src,dest,helper);
System.out.println ("transfer "+ n+" from "+src+" to "+dest);
tower(n-1,helper,src,dest);
}
public static void main(String[] args) {
//tower of hanoi
int n=3;
//towers be s,h,d;
tower (n,"S","H","D");
}
}
/* c version
#include <stdio.h>
void tower(int n,char *s,char *h,char *d){
if (n==1){
printf("move %d from %c to %c \n",n,*s,*d);
return;
}
tower(n-1,s,d,h);
printf("move %d from %c to %c \n",n,*s,*d);
tower(n-1,h,s,d);
}
int main() {
int n=3;
tower(n,"a","b","c");
}
*/