经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
【算法】Fibonacci(斐波那契数列)相关问题
来源:cnblogs  作者:GeekDragon  时间:2018/10/25 9:37:10  对本文有异议

一、列出Fibonacci数列的前N个数

  1. using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Fibonacci
  2. {class Program
  3.     {static void Main(string[] args)
  4.         {
  5.             cal(20);
  6.             cal2(20);//运行结果相同        }/*需求:列出Fibonacci数列的前N个数*///方案一:迭代N次,一次计算一项public static void cal(int N)
  7.         {int f1 = 1;int f2 = 1;int f = 0;for (int i = 1; i <= N; i++)
  8.             {if (== 1)
  9.                 {
  10.                     Console.Write(f1);
  11.                     Console.Write(" ");
  12.                 }else if (== 2)
  13.                 {
  14.                     Console.Write(f2);
  15.                 }else{
  16.                     f = f1 + f2;
  17.                     f2 = f1;
  18.                     f1 = f;
  19.                     Console.Write(" ");
  20.                     Console.Write(f);
  21.  
  22.                 }
  23.             }
  24.         }//方案二:迭代N/2次,一次计算两项public static void cal2(int N)
  25.         {int f1 = 1;int f2 = 1;for (int i = 1; i <= N / 2; i++)
  26.             {
  27.                 Console.Write(" ");
  28.                 Console.Write(f1);
  29.                 Console.Write(" ");
  30.                 Console.Write(f2);
  31.  
  32.                 f1 = f1 + f2;
  33.                 f2 = f2 + f1;
  34.             }
  35.         }

二、求出Fibonacci数列第N个数字(递归)

  1. namespace Fibonacci
  2. {class Program
  3.     {static void Main(string[] args)
  4.         {string N = Console.ReadLine();int integer = Convert.ToInt32(N);
  5.             Console.WriteLine("{0}",F(integer));
  6.  
  7.         }/*需求:求出Fibonacci数列第N个数字*///递归public static int F(int N)
  8.         {if (== 1)return 1;if (== 2)return 1;elsereturn F(- 1) + F(- 2);
  9.         }
  10.     }
  11. }

三、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

问题分析:其兔子数目依旧是按1,1,2,3,5…的顺序进行排列。其本质仍为斐波那契数列。

解决:按照一中的方案二进行即可。

  1. namespace Fibonacci
  2. {class Program
  3.     {static void Main(string[] args)
  4.         {//与方案二相同string N = Console.ReadLine();int integer = Convert.ToInt32(N);int f1 = 1;int f2 = 1;for (int i = 1; i <= integer; i++)
  5.             {
  6.                 Console.Write("{0} {1}",f1,f2);
  7.                 Console.Write(" ");if (i%2==0)
  8.                     Console.WriteLine("\n");
  9.                 f1 = f1 + f2;
  10.                 f2 = f2 + f1;
  11.             }
  12.         }
  13.         
  14.     }
  15. }

若后续有相关问题,余则继续补充。

愿诸位朋友及时指正!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号