1.汉诺塔问题
/*hanoi(汉诺塔问题)*/ #include <stdio.h> void move(char getone,char putone) { printf("%c->%c\n",getone,putone); } void hanoi(int n,char one,char two,char three) /*将n个盘子从one借助two,移到three*/ { if(n == 1) { move(one,three); } else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } int main() { int m; printf("please input a number:"); printf("%d",&m); printf("the step to moving %d disks :\n",m); hanoi(m,'A','B','C'); return 0; }2.利用递归函数调用方式,将所输入的n个字符以相反顺序打印出来 /*利用递归函数调用方式,将所输入的n个字符以相反顺序打印出来*/ #include <stdio.h> void print_char(int n) { char a; if(n <= 0) { a=getchar(); putchar(a); } else { a=getchar(); print_char(n-1); //当输入不止一个字符时,先进后出输出 putchar(a); } } int main() { int i; printf("please input n:"); scanf("%d",&i); print_char(i); return 0; }3.写一个函数实现9*9乘法表
/*输出9*9乘法表:要求输出格式为: 1 2 3 4 5 6 7 8 9 - - - - - - - - - 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 ... 9 18 27 36 ............81 */ #include <stdio.h> int main() { int i = 0; int j = 0; int a[9][9]; for ( i = 0;i < 9 ;i++ ) { for ( j = 0;j < 9;j++ ) { a[i][j] = ( i + 1 )*( j + 1 ); //实现乘法运算 } } printf("1 2 3 4 5 6 7 8 9\n"); printf("- - - - - - - - -\n"); for ( i = 0;i < 9;i ++ ) //输出乘法表 { for ( j = 0;j <= i ;j ++ ) { printf("%d ",a[i][j]); } printf("\n"); } return 0; }