Tower C and Tower C
/* Tower play: * game rule: Move the plate on column A to column C, and the tray must be on a small disk. * 1 when there is only one plate on column A, move it directly to column C; * 2 when there are two plates on column A, * move one disk on column A (numbered from top to bottom) move Column B, * move two disks on column A to column C, * move one disk on column B to column C. * (move 1 to column ~ N-1 disk ----> Column B, place column A n ----> column C, column B 1 ~ N-1 disk ----> C column) * 3 when there are three plates on the column ~ 2 disks move to Column B, * 3 disks on column A to column C, and * 1 to Column B ~ Move two disks to column C * (move 1 ~ N-1 disk ----> Column B, place column A n ----> column C, column B 1 ~ N-1-1 Disk ----> C column) * n when there are n plates on column A, 1 ~ N-1 disk is moved to Column B, * n disk on column A is moved to column C, and * 1 ~ N-1-1 disk is moved to the C-column. * (Set 1 ~ N-1 disk ----> Column B, place column A n ----> column C, column B 1 ~ N-1 disk ----> C column )**/
1 #include<stdio.h> 2 3 void Hanoi(int count,char a,char b,char c){ 4 if(count == 1){ 5 printf("FROM %c TO %c\n",a,c); 6 }else 7 { 8 Hanoi(count-1,a,c,b); 9 printf("FROM %c TO %c\n",a,c);10 Hanoi(count-1,b,a,c);11 }12 }13 int main(){14 printf("please input the number of Hanoi:");15 int n;16 scanf("%d",&n);17 Hanoi(n,'A','B','C');18 return 0;19 }