using System.Numerics;
计算 斐波那契数列(Fibonacci sequence),不受长整型位数限制。
编写 fibonacci.cs 如下
// C# program for Fibonacci Series
// using Dynamic Programming
using System;
using System.Numerics;
class fibonacci {
static BigInteger fib(int n)
{
BigInteger a = new BigInteger(0);
BigInteger b = new BigInteger(1);
BigInteger c = new BigInteger(0);
int i;
for (i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
// Fibonacci Series test
public static void Main(string[] args)
{
if (args.Length <1){
Console.WriteLine(" usage: fib.exe n ");
return;
}
int n;
if (int.TryParse(args[0], out n)){
if (n >1) Console.WriteLine(fib(n));
} else {
Console.WriteLine(" input n must be +int ");
}
}
}
where csc
C:\Windows\Microsoft.NET\Framework\v4.0.

最低0.47元/天 解锁文章
184

被折叠的 条评论
为什么被折叠?



