1,1,2,5,8,13,21
这样的数的求各的fuction怎么写
请大家不要笑我,我真的比较菜呀

解决方案 »

  1.   

    正中下怀~正好以前写过,一行就能搞定 private static readonly double sqrt5 = Math.Sqrt(5);
    /// <summary>
    /// 获得Fibonacci数列的第n个数n=1和n=2时都返回1。
    /// </summary>
    /// <param name="n">要获得数的序号(从1开始)</param>
    /// <returns>数列的第n个数</returns>
    public static int Get(int n)
    {
    return (int)Math.Round((Math.Pow( (1+sqrt5)/2, n) - Math.Pow( (1-sqrt5)/2, n ))/sqrt5);
    }
      

  2.   

    int a=1,b=1;
    int fun()
    {
      c = a + b;
      a = b;
      b = c;
      return c;
    }
    虽然有四行,可是还是比楼上的简单,呵呵第一次返回2,a=1,b=2,
    第二次返回3,( 你的题目好像漏了一个数) a=2,b=3
    第三次返回5,3,5
    四8,5,8
    13,8,13
    21,13,21
      

  3.   

    int function getn(int n)
    {
    int t;
    if(n=0)
    {
      t = 1;
    }
    else
    {
     t = getn(n-1)+n;
    }
    return t;
      }