新的Blog

.NET

数列:1,1,2,3,5,8,13,21,34,..... 求第50个数值? 并用c#写出算法?(不难,但要注意int 溢出问题)

[STAThread]
  static void Main(string[] args)
  {
             int n = int.Pares(System.Console.ReadLine());
            int i;
            Int64 j, k, temp; //如果用int则会得到: -298632863
            j = 1;
            k = 1;
            temp = 0;
            if (n <= 2)
            {
                Console.WriteLine("Number:{0}", k);
            }
            else
            {
                for (i = 0; i < n - 2; i++)
                {
                    temp = k;
                    k = k + j;
                    j = temp;
                }
                Console.WriteLine("Number:{0}", k);
            }

    }

posted on 2006-09-01 15:33  tity1110  阅读(2946)  评论(3编辑  收藏  举报

导航