看看这个,(*^__^*) 嘻嘻……,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 求阶乘
{
class Program
{
//计算每个数的阶乘,递归函数
static double f(int n)
{
if (n == 1)
return 1;
else
return n * f(n - 1);
}
//计算s(n)=1!+2!+3!+****+n!
static double S(int n)
{
double total = 0;
for (int i = 1; i <= n; i++)
{
total += f(i);
}
return total;
}
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入n值:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}!的结果为{1}",n, S(n));
}
}
}
}