C#实现大数相乘
代码
public string zz(string a, string b)
{
char[] chara = a.ToCharArray();
char[] charb = b.ToCharArray();
int[] inta = Array.ConvertAll(chara, new Converter<char, int>(delegate(char g) { return int.Parse(g.ToString()); }));
int[] intb = Array.ConvertAll(charb, new Converter<char, int>(delegate(char g) { return int.Parse(g.ToString()); }));
int x = a.Length;
int y = b.Length;
int[] c = new int[x + y];
while (x > 0) {
x--;
y = b.Length;
while (y > 0) {
y--;
c[x + y + 1] = c[x + y + 1] + inta[x] * intb[y];
}
}
string f = string.Join("", Array.ConvertAll(zzz(c, c.Length - 1), new Converter<int, string>(delegate(int g) { return g.ToString(); })));
return f.Substring(0, 1).Replace("0", "") + f.Substring(1, f.Length - 1);
}
public int[] zzz(int[] arr, int n)
{
if (n > 0)
{
arr[n - 1] += arr[n] / 10;
arr[n] %= 10;
return zzz(arr, n - 1);
}
return arr;
}
{
char[] chara = a.ToCharArray();
char[] charb = b.ToCharArray();
int[] inta = Array.ConvertAll(chara, new Converter<char, int>(delegate(char g) { return int.Parse(g.ToString()); }));
int[] intb = Array.ConvertAll(charb, new Converter<char, int>(delegate(char g) { return int.Parse(g.ToString()); }));
int x = a.Length;
int y = b.Length;
int[] c = new int[x + y];
while (x > 0) {
x--;
y = b.Length;
while (y > 0) {
y--;
c[x + y + 1] = c[x + y + 1] + inta[x] * intb[y];
}
}
string f = string.Join("", Array.ConvertAll(zzz(c, c.Length - 1), new Converter<int, string>(delegate(int g) { return g.ToString(); })));
return f.Substring(0, 1).Replace("0", "") + f.Substring(1, f.Length - 1);
}
public int[] zzz(int[] arr, int n)
{
if (n > 0)
{
arr[n - 1] += arr[n] / 10;
arr[n] %= 10;
return zzz(arr, n - 1);
}
return arr;
}