zoukankan      html  css  js  c++  java
  • C 语言实例

    C 语言实例 - 斐波那契数列
    
    
    
    斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233377610987159725844181676510946177112865746368........
    这个数列从第3项开始,每一项都等于前两项之和。
    实例 - 输出指定数量的斐波那契数列
    #include <stdio.h>
     
    int main()
    {
        int i, n, t1 = 0, t2 = 1, nextTerm;
     
        printf("输出几项: ");
        scanf("%d", &n);
     
        printf("斐波那契数列: ");
     
        for (i = 1; i <= n; ++i)
        {
            printf("%d, ", t1);
            nextTerm = t1 + t2;
            t1 = t2;
            t2 = nextTerm;
        }
        return 0;
    }
    运行结果:
    输出几项: 10
    斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
    
    
    实例 - 输出指定数字前的斐波那契数列
    #include <stdio.h>
     
    int main()
    {
        int t1 = 0, t2 = 1, nextTerm = 0, n;
     
        printf("输入一个正数: ");
        scanf("%d", &n);
     
        // 显示前两项
        printf("斐波那契数列: %d, %d, ", t1, t2);
     
        nextTerm = t1 + t2;
     
        while(nextTerm <= n)
        {
            printf("%d, ",nextTerm);
            t1 = t2;
            t2 = nextTerm;
            nextTerm = t1 + t2;
        }
        
        return 0;
    }
    运行结果:
    输入一个正数: 100
    斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
  • 相关阅读:
    规则引擎.Net Core
    GDPR(Cookie处理)
    NSSM把.Net Core部署至 Windows 服务
    Consul实现服务治理1
    微服务
    Consul实现服务治理
    NET Core Web发布包
    NET API 分析器
    NET Core 2.1 Global Tools
    css3中-moz、-ms、-webkit,-o分别代表的意思,以及微信浏览器内核分析
  • 原文地址:https://www.cnblogs.com/bytebee/p/8535728.html
Copyright © 2011-2022 走看看