使用数组计算大量乘积? [英] Calculating products of large numbers using arrays?

查看:92
本文介绍了使用数组计算大量乘积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project (for school) and I absolutely cant use any external libraries hence cannot use any big numbers library and I need to get the product of 2 (very) large numbers. So I thought I'll actually write my own code for it but I cant seem to get pass single digit multiplications.

到目前为止,我的工作方式是我有一个字符'a'数组.病态将其每个数字与另一个数字相乘(因为任何乘法都不能超过81,即9 * 9).但是我似乎无法弄清楚如何将两个数组彼此相乘.

How I've done it so far is I have an array of chars 'a'. And Ill multiply each of its digits with the other number (since no multiplication can go beyond 81 ie, 9*9). But I cant seem to figure out how Ill multiply two arrays with each other.

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

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

到目前为止,这里有我的代码...

Heres my code so far...

#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

}

我还找到了一个类似的程序

I also found a similar program here which calculates factorials of very large numbers. But I cant seem to understand the code enough to change it to my needs.

很抱歉,这个问题有点粗略.

Sorry if the question is a little sketchy.

谢谢.

推荐答案

我已经用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

I have done in java, Here I am taking to numbers N1 and N2, And I have create an array of size 1000. Lets take an example How to solve this, N1=12, N2=1234. For N1=12, temp=N1%10=2, Now Multiply this digit with digit N2 from right to Left and store the result into array starting from i=0, similarly for rest digit of N1. The array will store the result but in reverse order. Have a looking on this link. 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;
    }
}

这篇关于使用数组计算大量乘积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆