展会搭建注意事项:用C#写出s=1/1!-1/2!+1/3!+...+1/n!

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 21:31:22
请高手多多指点,我要的是详细的过程。也就是一个可以执行的程序。谢谢。

using System;
class Test
{
// 计算阶乘
private double Factorial ( int n )
{
double rtn = 1;
while ( n > 1 )
{
rtn *= n -- ;
}
return rtn;
}
//计算s=1/1!-1/2!+1/3!+...+1/n!
public double Foo ( int n )
{
double rtn = 0 ;

while ( n > 0 )
{
rtn += 1/(Factorial(n--));
}
return rtn;
}
[STAThread]
static void Main(string[] args)
{
System.Console.WriteLine ( new Test().Foo( 5 ) );
}
}

很简单了,不用说什么了吧