acm练习了两道题目,c语言函数通过汉诺塔和青蛙跳台的问题巩固了递归的使用方法
#define ?_CRT_SECURE_NO_WARNINGS #include <stdio.h>
//青蛙跳台问题 int qw(int x) { ?? ?if (x == 1) ?? ??? ?return 1; ?? ?else if (x == 2) ?? ??? ?return 2; ?? ?else ?? ??? ?return qw(x - 1) + qw(x - 2); }
int main() { ?? ?int a = 0; ?? ?scanf("%d", &a); ?? ?printf("%d\n", qw(a)); ?? ?return 0; }
// 汉诺塔问题 //int hnt(int x) //{ //?? ?if (x > 1) //?? ??? ?return 2 + hnt(x - 1); //?? ?else //?? ??? ?return 1; //} //int main() //{ //?? ?int a = 0; //?? ?scanf("%d", &a); //?? ?printf("%d\n",hnt(a)); //?? ?return 0; //}
求第n个斐波那契数的迭代写法 //int fib(int x) //{ //?? ?if (x <= 2) //?? ??? ?return 1; //?? ?else? //?? ?{ //?? ??? ?int a = 1; //?? ??? ?int b = 1; //?? ??? ?int c = 2; //?? ??? ?for (int i = 3; i <= x; i++) //?? ??? ?{ //?? ??? ??? ?c = a + b; //?? ??? ??? ?a = b;? //?? ??? ??? ?b = c; // //?? ??? ?} //?? ??? ?return c; //?? ?} //} //int main() //{ //?? ?int a = 0; //?? ?scanf("%d", &a); //?? ?printf("%d\n", fib(a)); //?? ?return 0; //} //
求第n个斐波那契数的递归写法 //int fib(int x) //{ //?? ?if (x >= 3) //?? ??? ?return fit(x - 1) + fit(x - 2); //?? ?else //?? ??? ?return 1; //} // //int main() //{ //?? ?int a = 0; //?? ?scanf("%d", &a); //?? ?printf("%d\n", fib(a)); //?? ?return 0; //} // // //
求n的阶乘 //int fac(int x) //{ //?? ?if (x > 1) //?? ??? ?return x*fac(x - 1); //?? ?else //?? ??? ?return 1; //} //int main() //{ //?? ?int a = 0; //?? ?scanf("%d", &a); //?? ?printf("%d\n", fac(a)); //}
int main() { ?? ?int a = 0; ?? ?int b = getchar(a); ?? ?a = b - 32; ?? ?printf("%c\n", a); ?? ?return 0; }
//int main() //{ //?? ?int m = 0; //?? ?int t = 0; //?? ?int s = 0; //?? ?int a = 0; //?? ?scanf("%d%d%d", &m, &t, &s); //?? ?if (t == 0) //?? ??? ?printf("0"); //?? ?else //?? ?{ //?? ??? ?a = m - s / t; //?? ??? ?if (a > 0 && s%t==0) //?? ??? ??? ?printf("%d", a); //?? ??? ?else if (a>0 && s%t!=0) //?? ??? ??? ?printf("%d", a-1); //?? ??? ?else //?? ??? ??? ?printf("0"); //?? ?} //?? ?return 0; // //}
|