写一个算法思路可以利用32位计算机计算一下两个数之间的乘积

incompletewild 2010-03-15 10:51:08
b=42423408908534590893423408590820480923840928095829482342398402480234
a=14234235634298490859038409583058093485093850983095830509385038503805
c=a*b
c是多少
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianke917 2010-03-16
  • 打赏
  • 举报
回复
结果:603864798814279120185225586728269967994486053541840823568791676976796892171207357745308923065868664503808921702014434265317500446290370
jianke917 2010-03-16
  • 打赏
  • 举报
回复
利用求高精度数据之积,将两数据转换为数组,利用数字进位法则计算!

public void GetAmass()
{

//Console.WriteLine("请输入第一个数!");
//string a = Console.ReadLine();
//Console.WriteLine("请输入第二个数!");
//string b = Console.ReadLine();
string a = "42423408908534590893423408590820480923840928095829482342398402480234";
string b = "14234235634298490859038409583058093485093850983095830509385038503805";
int[] num1 = new int[a.Length+1];
int[] num2 = new int[b.Length+1];
int[] c= new int[a.Length+b.Length+1];
for (int i = 1; i <= a.Length; i++)
{
num1[i] = Convert.ToInt32(a.Substring(a.Length - i, 1));
}
for (int j = 1; j <= b.Length; j++)
{

num2[j] = Convert.ToInt32(b.Substring(b.Length - j, 1));
}
for (int i = 1; i <=a.Length; i++)
{
for (int j = 1; j <=b.Length; j++)
{
int x = num1[i] * num2[j];
int y = x / 10;//进位
int z = x % 10;//本位数
int w = i + j - 1;
c[w] += z;
c[w + 1] = c[w + 1] + y + c[w] / 10;
c[w] = c[w] % 10;
}
}

string s = null;
int l = a.Length + b.Length + 1;
while (c[l - 1] == 0)
{
l -= 1;
}
for (int i = l-1; i >=1 ; i--)
{
s += c[i].ToString();
}
Console.WriteLine(s);
Console.ReadLine();
}

incompletewild 2010-03-16
  • 打赏
  • 举报
回复
谢谢!
諾临風 2010-03-16
  • 打赏
  • 举报
回复
不错~
polarissky 2010-03-16
  • 打赏
  • 举报
回复
向空军学习
wuyi8808 2010-03-15
  • 打赏
  • 举报
回复
using System;
using System.Text;
using System.Collections.Generic;

sealed class BigInteger
{
static readonly int Len = 9; // int.MaxValue = 2,147,483,647
static readonly int Base = (int)Math.Pow(10, Len);

int sign;
List<int> data;

BigInteger(long x)
{
sign = (x == 0) ? 0 : ((x > 0) ? 1 : -1);
data = new List<int>();
for (ulong z = Abs(x); z != 0; z /= (ulong)Base) data.Add((int)(z % (ulong)Base));
}

BigInteger(BigInteger x)
{
sign = x.sign; // x != null
data = new List<int>(x.data);
}

public static implicit operator BigInteger(long x)
{
return new BigInteger(x);
}

public static BigInteger Parse(string s)
{
if (s == null) return null;
s = s.Trim().Replace(",", null);
if (s.Length == 0) return 0;
BigInteger z = 0;
z.sign = (s[0] == '-') ? -1 : 1;
if (s[0] == '-' || s[0] == '+') s = s.Substring(1);
int r = s.Length % Len;
z.data = new List<int>(new int[s.Length / Len + ((r != 0) ? 1 : 0)]);
int i = z.data.Count - 1;
if (r != 0) z.data[i--] = int.Parse(s.Substring(0, r));
for (; i >= 0; i--, r += Len) z.data[i] = int.Parse(s.Substring(r, Len));
z.Shrink();
return z;
}

public static void Swap(ref BigInteger x, ref BigInteger y)
{
BigInteger z = x;
x = y;
y = z;
}

public static ulong Abs(long x)
{
return (x < 0) ? (ulong)-x : (ulong)x;
}

public static BigInteger Abs(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = Math.Abs(x.sign);
return z;
}

public static BigInteger Pow(BigInteger x, int y)
{
if (x == null) return null;
BigInteger z = 1, n = x;
for (; y > 0; y >>= 1, n *= n) if ((y & 1) != 0) z *= n;
return z;
}

public static BigInteger operator +(BigInteger x)
{
if (x == null) return null;
return new BigInteger(x);
}

public static BigInteger operator -(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = -x.sign;
return z;
}

public static BigInteger operator +(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
if (x.AbsCompareTo(y) < 0) Swap(ref x, ref y);
BigInteger z = new BigInteger(x);
if (x.sign * y.sign == -1) z.AbsSubtract(y);
else z.AbsAdd(y);
return z;
}

public static BigInteger operator -(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
return x + (-y);
}

public static BigInteger operator *(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
BigInteger z = 0;
z.sign = x.sign * y.sign;
z.data = new List<int>(new int[x.data.Count + y.data.Count]);
for (int i = x.data.Count - 1; i >= 0; i--)
for (int j = y.data.Count - 1; j >= 0; j--)
{
long n = Math.BigMul(x.data[i], y.data[j]);
z.data[i + j] += (int)(n % Base);
z.CarryUp(i + j);
z.data[i + j + 1] += (int)(n / Base);
z.CarryUp(i + j + 1);
}
z.Shrink();
return z;
}

public static BigInteger operator /(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
return DivRem(dividend, divisor, out remainder);
}

public static BigInteger operator %(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
DivRem(dividend, divisor, out remainder);
return remainder;
}

public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder)
{
remainder = null;
if (dividend == null || divisor == null) return null;
if (divisor.sign == 0) throw new Exception("division by zero");
if (dividend.AbsCompareTo(divisor) < 0)
{
remainder = new BigInteger(dividend);
return 0;
}
remainder = 0;
BigInteger quotient = 0;
quotient.sign = dividend.sign * divisor.sign;
quotient.data = new List<int>(new int[dividend.data.Count]);
divisor = Abs(divisor); // NOT: divisor.sign = Math.Abs(divisor.sign);
for (int i = dividend.data.Count - 1; i >= 0; i--)
{
remainder = remainder * Base + dividend.data[i];
int iQuotient = remainder.BinarySearch(divisor, -1);
quotient.data[i] = iQuotient;
remainder -= divisor * iQuotient;
}
quotient.Shrink();
if (remainder.sign != 0) remainder.sign = dividend.sign;
return quotient;
}

public static BigInteger Sqrt(BigInteger x)
{
if (x == null || x.sign < 0) return null;
BigInteger root = 0;
root.sign = 1;
root.data = new List<int>(new int[x.data.Count / 2 + 1]);
for (int i = root.data.Count - 1; i >= 0; i--) root.data[i] = x.BinarySearch(root, i);
root.Shrink();
return root;
}

int BinarySearch(BigInteger divisor, int i)
{
int low = 0, high = Base - 1, mid = 0, cmp = 0;
while (low <= high)
{
mid = (low + high) / 2;
cmp = CompareTo(divisor, mid, i);
if (cmp > 0) low = mid + 1;
else if (cmp < 0) high = mid - 1;
else return mid;
}
return (cmp < 0) ? (mid - 1) : mid;
}

int CompareTo(BigInteger divisor, int mid, int i)
{
if (i >= 0) divisor.data[i] = mid;
return AbsCompareTo(divisor * ((i >= 0) ? divisor : mid));
}

void AbsAdd(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] += x.data[i];
CarryUp(i);
}
}

void AbsSubtract(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] -= x.data[i];
CarryDown(i);
}
Shrink();
}

void CarryUp(int n)
{
for (; data[n] >= Base; n++)
{
if (n == data.Count - 1) data.Add(data[n] / Base);
else data[n + 1] += data[n] / Base;
data[n] %= Base;
}
}

void CarryDown(int n)
{
for (; data[n] < 0; n++)
{
data[n + 1]--;
data[n] += Base;
}
Shrink();
}

void Shrink()
{
for (int i = data.Count - 1; i >= 0 && data[i] == 0; i--) data.RemoveAt(i);
if (data.Count == 0) sign = 0;
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (sign < 0) sb.Append('-');
sb.Append((data.Count == 0) ? 0 : data[data.Count - 1]);
for (int i = data.Count - 2; i >= 0; i--) sb.Append(data[i].ToString("D" + Len));
return sb.ToString();
}

public int CompareTo(BigInteger other)
{
if (object.ReferenceEquals(other, null)) return 1;
if (sign < other.sign) return -1;
if (sign > other.sign) return 1;
if (sign == 0) return 0;
return sign * AbsCompareTo(other);
}

int AbsCompareTo(BigInteger other)
{
if (data.Count < other.data.Count) return -1;
if (data.Count > other.data.Count) return 1;
for (int i = data.Count - 1; i >= 0; i--)
if (data[i] != other.data[i])
return (data[i] < other.data[i]) ? -1 : 1;
return 0;
}
}

class Program
{
static void Main()
{
BigInteger b = BigInteger.Parse("42423408908534590893423408590820480923840928095829482342398402480234");
BigInteger a = BigInteger.Parse("14234235634298490859038409583058093485093850983095830509385038503805");
BigInteger c = a * b;
Console.WriteLine(c);
// 603864798814279120185225586728269967994486053541840823568791676976796892171207357745308923065868664503808921702014434265317500446290370
}
}
wuyi8808 2010-03-15
  • 打赏
  • 举报
回复
BigInteger Class

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧