【问题标题】:Calculating products of large numbers using arrays?使用数组计算大量乘积?
【发布时间】:2013-08-21 12:51:34
【问题描述】:

我有一个项目(用于学校),我绝对不能使用任何外部库,因此不能使用任何大数库,我需要获得 2 个(非常)大数的乘积。所以我想我实际上会为它编写自己的代码,但我似乎无法通过个位数乘法。

到目前为止,我所做的是我有一个字符数组'a'。并且将其每个数字与另一个数字相乘(因为乘法不能超过 81,即 9*9)。但我似乎无法弄清楚如何将两个数组相乘。

如,

int a[] = {1,2,3};
int b[] = {4,5,6};

int r[200]; // To store result of 123x456. After processing should have value 56088

这是我目前的代码...

#include <iostream>
using namespace std;

void reverseArray(int array[], int n)
{
    int t;
    for(int i=0;i<n/2;i++)
    {
        t = array[i];
        array[i] = array[n-i-1];
        array[n-i-1] = t;
    }
}

int main()
{
    int A[] = {1,2,6,6,7,7,8,8,8,8,8,8,8,8,8,8};
    int s = sizeof(A)/sizeof(int);
    int n = s-1;

    int R[50];


    int x = 2;

    int rem = 0;

    for(int i=0; i<s; i++)
    {
        R[i] = (A[n-i] * x) % 10;
        R[i] += (rem != 0) ? rem:0;
        rem = (A[n-i] * x) / 10;
    }

    reverseArray(R, s);

    for(int i=0; i<s; i++) cout<<R[i]; // Gives 2533557777777776

}

我还发现了一个类似的程序here,它计算非常大的数的阶乘。但我似乎无法充分理解代码以将其更改为我的需要。

对不起,如果问题有点粗略。

谢谢。

【问题讨论】:

  • 你将如何用手仅用笔和纸将 2 个非常大的数字(例如 20 位数字)相乘(如小学所教)?您可以在这里应用相同的想法。

标签: c++ math


【解决方案1】:

只需像现在一样做同样的事情,但对于第二个数组中的每个数字 - 换句话说,而不是 x 使用 B[j],其中 j 是对数组中所有数字的循环数组B

【讨论】:

    【解决方案2】:

    我不确定你在小学学了什么算法来将两个任意数相乘,但从视觉上看是这样的:

       43241
         621
       ----- *
       43241 <--   1 * 43241
      864820 <--  20 * 43241, basically do 2 * 43241 and append a zero
    25944600 <-- 600 * 43241, basically do 6 * 43241 and append two zeroes
    -------- +
    26852661 <-- Add up results, remember to carry
    

    因此,在这个特定示例中,数组将是 A[] = {1,4,2,3,4}B[] = {1,2,6}。然后你可以做一个 for 循环,比如

    int tempArray[50]; // something big enough
    for (int n = 0; n < max; n++)
    {
        multiplyArrayWithNumber(A, B[i], tempArray, i);
        addArraysAndStore(resultArray, tempArray, resultArray);
    }
    

    函数multiplyArrayWithNumberaddArraysAndStore 可能具有签名的位置

    void multiplyArrayWithNumber(const int* array, const int number, int* resultArray, const int zeroesAppended);
    
    void addArraysAndStore(const int* lefthandside, const int* righthandside, int* result);
    

    【讨论】:

      【解决方案3】:

      我在java中做过,这里我取的是数字N1和N2,我创建了一个大小为1000的数组。让我们举个例子如何解决这个问题, N1=12,N2=1234。 对于 N1=12,temp=N1%10=2,现在将此数字与数字 N2 从右到左相乘,并将结果存储到从 i=0 开始的数组中,对于 N1 的其余数字也是如此。该数组将存储结果,但顺序相反。看看这个链接。 http://ideone.com/UbG9dW#view_edit_box

      //Product of two very large number
      import java.io.*;
      import java.util.*;
      import java.text.*;
      import java.math.*;
      import java.util.regex.*;
      
       class Solution {
          public static void main(String[] args) {
              Scanner scan=new Scanner(System.in);
              int N1=scan.nextInt();
              int N2=scan.nextInt();
              //int N=scan.nextInt();
              int [] array=new int[1000];
              Arrays.fill(array,0);
              int size=multiply(N1,N2,array);
              for(int i=size-1;i>=0;i--){
                  System.out.print(array[i]);
              }
          }
          public static int multiply(int N1, int N2, int [] result){
              int a=N1;
              int b=N2;            
              int count=0, carry=0;
              int i=0;
              int max=0;
              if(a==0||b==0)
                  return 1;
              while(a>0){
                  int temp1=a%10;
                  a=a/10;
                  i=0;
                  while(b>0){
                      int temp2=b%10;
                      b=b/10;
                      int product=result[count+i]+temp1*temp2+carry;
                      result[count+i]=product%10;
                      carry=product/10;
                      i++;
                      //System.out.println("ii="+i);
                  }
                  while(carry>0){
                      result[count+i]=carry%10;
                      carry=carry/10;
                      i++;
                      //System.out.println("iiii="+i);
                  }
                  count++;
                  b=N2;
              } 
              //System.out.println("i="+i);
      
              return i+count-1;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-07
        • 2015-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多