改动了一下

来源:互联网 发布:五笔查询软件 编辑:程序博客网 时间:2024/05/17 04:27

今天没事,改了下

using System;

namespace ConsoleApplication5
{
class Class1
{
/* 此问题即高精度算法,常用于银行,也可以用来算PI后几百位*/


static int[] factorial(int n) //阶乘算法,采用一维组数来保存
{
const int arraynum=200; //数组维数, 如果是1000的阶乘,变动此数即可
int[] fac = new int[arraynum]; //结果数组
int[] add = new int[arraynum]; //进位数组
for(int i=0; i<arraynum; i++) //初始化结果数组,第一个数为1, 即10000.....
fac[i]=0;
fac[0]=1;

for(int i=0; i<arraynum; i++) //初始化时位数组为0,即000000
add[i]=0;

for (int r=1;r<=n; r++) //迭代开始,即1*2*3*....n
{
for (int k=1;k<arraynum;k+=2)
{
add[k]=(fac[k-1] *r + add[k-1]) /10; //进位数组赋值,进位是从1开始
k--;
fac[k]=(fac[k] * r + add[k]) % 10; //结果数组赋值,结果是从0开始
}
}

return fac; //返回结果数组
}

static void Main(string[] args)
{
try
{
Console.WriteLine("请输入1-100的数字!");
String temp = Console.ReadLine();
int n= Int32.Parse(temp);
if(n<1||n>100)
throw new Exception("不是1-100范围的数字");

int[] result=factorial(n);

//逆向输出
for (int i=result.Length-1;i>=0;i--)
{
if(result[i]==0) continue;//去掉数组前面是0的元素
for(;i>=0;i--)
{
Console.Write("{0}",result[i]);
}
}
}

catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadLine();
}

//程序结束

}
}
}