//Util.cs文件
using System;
namespace CompleteFactorial
{
/// <summary>
/// Summary description for Util.
/// </summary>
public class Util
{
public Util(){}
/// <summary>
/// 定义每个数组存储的最大值
/// </summary>
const int UNIT=100;
/// <summary>
/// 定义数组的最大位置
/// </summary>
static int count=1;
/// <summary>
/// 阶乘方法
/// </summary>
/// <param name="n">输入的阶乘数</param>
public static void Factorial(int n)
{
///分配临时空间
int[] array=new int[100];
///初始化数组0位的值
array[0]=1;
count=1;
while(n>0)
{
Dohandle(array,n);
n--;
}
Console.WriteLine("Count = "+count);
///打印数据
Console.Write("阶乘计算结果 = ");
for(int i=count;i>-1;i--)
{
Console.Write(array[i].ToString("00"));
}
/* 阶乘计算结果 = 933262154439441526816992388562667004907159682643816214685929638952175999932
29915608941463976156518286253697920827223758251185210916864000000000000000000000000 */
}
/// <summary>
/// 处理阶乘
/// </summary>
/// <param name="array">数组</param>
/// <param name="n">要乘的数</param>
private static void Dohandle(int[] array,int n)
{
for(int i=count;i>-1;i--)
{
array[i]*=n;
///如果超出最大存储的值
if(array[i]>UNIT)
{
//进位
array[i+1]+=array[i]/UNIT;
array[i]=array[i]%UNIT;
///count记住数组最大位置
if(i>=count)
{
count=i+1;
}
}
}
}
}
}
//Main.cs
using System;
namespace CompleteFactorial
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class FactorialMain
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Util.Factorial(100);
Console.ReadLine();
}
}
}