c# 求一定的大阶乘
1.通过List<int> 类型存储
代码:
private List<int> fact(int n)
{
int s = 1;
int m = 0;
int a,b,c;
List<int> lstfact = new List<int>();
List<int> lstresult = new List<int>();
lstfact.Add(1);
for (int i = 1; i <= n; i++)
{
m = 0;
for (int k = 0; k < lstfact.Count;k++ )
{
s = i * lstfact[k];
s = s + m;
a = s / 10;
b = s % 10;
lstfact[k] = b;
if (a>0)
{
if (k<lstfact.Count-1)
{
m = a;
}
else
{
lstfact.Add(a);
break;
}
}
else
{
m = 0;
}
}
}
for (int k = lstfact.Count - 1; k >= 0;k-- )
{
lstresult.Add(lstfact[k]);
}
return lstresult;
}
2.通过字符串存储
private string factss(int n)
{
int s = 1;
int m = 0;
int a, b, c;
StringBuilder strb = new StringBuilder();
StringBuilder strfac = new StringBuilder();
strb.Append("1");
for (int i = 1; i <= n; i++)
{
m = 0;
for (int k =0; k<strb.Length; k++)
{
s = i * int.Parse(strb[k].ToString());
s = s + m;
a = s / 10;
b = s % 10;
strb[k] =Convert.ToChar(b.ToString());
if (a > 0)
{
if (k < strb.Length - 1)
{
m = a;
}
else
{
strb.Append(a);
break;
}
}
else
{
m = 0;
}
}
}
for (int k =strb.Length-1; k >= 0; k--)
{
strfac.Append(strb[k]);
}
return strfac.ToString();
}