大数阶层的算法 C#

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 20:06:43

大数阶层的算法 C#
大数阶层的算法 C#

大数阶层的算法 C#
这里写了个1到10000的阶乘的代码
如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string result = Factorial(10);
Console.Write(result);
}
public static string Factorial(int end)
{
///用于存放
ArrayList ints = new ArrayList();
ints.Add(end);
end--;
for (int n = end; n > 1; n--)
{
StringBuilder str = new StringBuilder();
///遍历数组乘法运算
for (int m = 0; m < ints.Count; m++)
{
if (Convert.ToInt32(ints[m]) != 0)
{
ints[m] = (Convert.ToInt32(ints[m]) * n).ToString();
}
}
///将数组每个元素变成5位数字
for (int m = 0; m < ints.Count - 1; m++)
{
string nowStr = ints[m].ToString();
if (nowStr.Length > 5)
{
int beg = Convert.ToInt32(nowStr.Substring(0,nowStr.Length - 5));
int last = Convert.ToInt32(ints[m + 1]);
ints[m + 1] = last + beg;
ints[m] = nowStr.Substring(nowStr.Length - 5,5);
}
}
///将数组转换成字符串
for (int m = (ints.Count - 1); m >= 0; m--)
{
str.Append(ints[m]);
}
///清除数组
ints.Clear();
///将字符串变成数组
int intsCount = (str.Length % 5 > 0) (str.Length / 5 + 1) :str.Length / 5;
///字符串
string result = str.ToString();
///切割到数组
int begin = str.Length - (intsCount - 1) * 5;
if (begin != 0)
{
ints.Add(result.Substring(0,begin));
}
for (int m = 0; m < intsCount - 1; m++)
{
ints.Insert(0,result.Substring(m * 5 + begin,5));
}
}
StringBuilder results = new StringBuilder();
///将数组每个元素变成5位数字
for (int m = 0; m < ints.Count - 1; m++)
{
string nowStr = ints[m].ToString();
if (nowStr.Length > 5)
{
int beg = Convert.ToInt32(nowStr.Substring(0,nowStr.Length - 5));
int last = Convert.ToInt32(ints[m + 1]);
ints[m + 1] = last + beg;
ints[m] = nowStr.Substring(nowStr.Length - 5,5);
}
}
///将数组转换成字符串
for (int m = (ints.Count - 1); m >= 0; m--)
{
results.Append(ints[m]);
}
return results.ToString();
}
}
}