C语言学习Day12

#define _CRT_SECURE_NO_WARNINGS 1

/****

  • @Title:> Day12
  • @Description:> 递归
  • @author:> HuaGe
  • @date:> 2020/10/20 18:56
    ***/

//递归
//#include<stdio.h>
////题:接受一个整形值,按照顺序打印它的每一位,如输入123,输出1 2 3.
//void int_Print(int x)
//{
// if (x > 9) {
// int_Print(x / 10);
// }
// printf("%d\n", x % 10);
//}
//
//int main()
//{
// printf("请输入一个整形数:");
// int a;
// scanf("%d", &a);
// int_Print(a);
//
// return 0;
//}

#include<stdio.h>
////求字符串长度函数
//int my_StrLen(char arr)
//{
// int mark = 0;
// while (
arr != '\0'){
// mark++;
// arr++;
// }
// return mark;
//}

////用递归的方式求字符串长度
//int my_StrLen(char arr)
//{
// if (
arr != '\0') {
// return my_StrLen(arr+1) + 1;
// }
// else {
// return 0;
// }
//}
//
//int main()
//{
// char arr[] = "ertere";
// int len = my_StrLen(arr);
// printf("%d\n", len);
//
// return 0;
//}

////用递归求n的阶乘
//int jieCheng(int n)
//{
// if (n == 1) {
// return 1;
// }
// else {
// return n * jieCheng(n - 1);
// }
//}
//
//int main()
//{
// int n = 4;
// int result = jieCheng(n);
// printf("%d\n", result);
//
// return 0;
//}

//求斐波那契数列,用递归不好,重复计算太大。
int FeiB(int n)
{
int a, b;
a = 1;
b = 1;
int result = 1;
//printf("a=%d\tb=%d\tc=%d\n", a, b, c);
/if (n == 1 || n == 2) {
return 1;
}
/
for (int i = 2; i < n; i++) {
result = a + b;
a = b;
b = result;
}
return result;
}

int main()
{
int n = 6;
int result = FeiB(n);
printf("%d\n", result);

return 0;

}

猜你喜欢

转载自blog.51cto.com/14947752/2542680