C语言——计算Fibonacci数列

方式一

for循环 (20位)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
int main()
{
    int n;
    int a[20]={1,1};
    for ( n = 1; n <20; n++)
    {
        a[n]=a[n-2]+a[n-1];
    }
    for ( n = 0; n < 20; n++)
    {
        if(n%5==0)
        printf("\n");
        printf("%12d ",a[n]);
    }
    return 0;
}
 

方式二

用递归法编程

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
long Fib(int n);
int main()
{
    int n,i,x;
    printf("Input n:");
    scanf("%d",&n);
    for ( i = 1; i <=n; i++)
    {
        x=Fib(i);
        printf("Fi(%d) = %d\n",i,x);
    }
    return 0;
}
long Fib(int n)
{
    if (n == 0)  return 0;
    else if (n ==1)  return 1;
    else return (Fib(n-1)+Fib(n-2));

    return 0;
}
 
相关推荐
生态学者4 小时前
Global Change Biology | 森林越“步调一致”,反而越怕干旱?树木生长与叶片活动的同步之谜
大数据·人工智能·算法·r语言·微信公众平台
所以遗憾是什么呢?4 小时前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
lsswear5 小时前
Agent 框架新手入门与实战部署指南
大数据·人工智能·算法
学逆向的5 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
Lhan.zzZ6 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
Data analyse4566 小时前
rnweb 回放数据量太大怎么办?分片存储与断点续传的实现思路
前端·网络·算法·数据分析
sysu_chenxy8 小时前
二重循环输出二维图形
c++
Navigator_Z8 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil2088 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年8 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构