递归与迭代 C语言练习题
目录
1. 斐波那契数列 1 1 2 3 5 8 13 21 34 即F(n)+F(n+1)=F(n+2)
说明:
1. 当 n <= 2 时,结果为 1;
2. 当 n > 2 时,结果为F(n)+F(n+1)=F(n+2);
想法 :
1. 当 n <= 2 时,结果为 1;
2. 当 n > 2 时,结果为F(n)+F(n+1)=F(n+2);
注 : 在2019当中 scanf 函数提示有风险警告,添加下面预处理命令后方可使用 scanf 函数
#define _CRT_SECURE_NO_WARNINGS 1 //scanf 函数的未识别错误处理,否则使用 scanf_s 函数名
#include <stdio.h>
使用递归效率太低
//int F(int n)
//{
// if (n <= 2)
// return 1;
// else
// return F(n - 1) + F(n - 2);
//}
//
//int main()
//{
// int n = 0;
// int result = 0;
// scanf("%d",&n);
// //第N个斐波那契数列的值为
// result = F(n);
// printf("%d",result);
//
// return 0;
//}
##### 使用迭代
1. 前两个数相加为第三个数的值 从前往后计算
2. while进行循环
#define _CRT_SECURE_NO_WARNINGS 1 //scanf 函数的未识别错误处理,否则使用 scanf_s 函数名
#include <stdio.h>
int F(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n