求C#编程1!+2!+3!+4!+5!
谢谢啦
程序代码:
int count = 0; for (int i = 1; i <= 5; i++) { count += i; }
static void Main(string[] args) { Console.WriteLine(GetValues(5)); Console.ReadLine(); } public static int GetValues(int n) { int Num = 0; for (int i = 0; i < n; i++) { Num += Foo(n); } return Num; } public static int Foo(int n) { if (n > 0) { return Foo(n - 1) * n; } else { return 0; } }
static void Main(string[] args) { Console.WriteLine(GetValues(5)); Console.ReadLine(); } public static int GetValues(int n) { int Num = 0; for (int i = 0; i < n; i++) { Num += Foo(n); } return Num; } public static int Foo(int n) { if (n > 0) { return Foo(n - 1) * n; } else { return 1; } }刚才发快了,没注意判断了,Foo函数,当N<0时,返回1,按理说负数是没有阶乘的!