求一个最经典的阶乘算法(c#)

来源:百度知道 编辑:UC知道 时间:2024/06/27 13:56:44
大家帮我写一个最经典的阶乘算法!

int jiecheng(int n)
{
if(n==1) return 1;
else return n*jiecheng(n-1);
}

1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
....../
自己去探索吧

得考虑大数的阶乘如何组织

public int jiecheng(int i)
{
return (i == 1 || i == 0) ? 1 : i * jiecheng(i - 1);

}

大数的阶乘就麻烦了,要用string或数组存,是一道经典的算法题啊

C# 控制台程序

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Class1
{
private static void Main()
{
Console.Write("Please input num N:");
int _n = 0;
try//有可能输入字符,所以要try一下
{
_n = int.Parse(Console.ReadLine());
}
catch
{ }
if (_n > 0)
Console.WriteLine(factorial(_n));