作业:如何写自己的大数字乘法? [英] Homework: how to write own multiplication of big numbers?

查看:135
本文介绍了作业:如何写自己的大数字乘法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须处理在我自己的 BigNumber 类中的大数字(大于java.long)的乘法,因为 int [ ] 。基本上我需要实现这样的事情:

In my project I have to deal with multiplication of big numbers ( greater then java.long ) stared in my own BigNumber class as int[]. Basically I need to implement something like this :

    157 x
    121 y
   ----
    157 result1
   314  + result2
  157   + result3
 ------
 18997  finalResult

但我该如何实现呢?

我想用零(3140,15700)扩展result2,3并添加它们。但首先我需要在y的每个数字之间导航并乘以x的每个数字。

I thought about expanding result2,3 with zeros (3140, 15700) and adding them. But first I somehow need to navigate between each digit of y and multiply it by each digit of x.

推荐答案

使用对角线方法。创建一个数组,并将每个数字乘以每个其他数字并填写每个单元格中的数字。

Use the diagonal approach. Make an array, and multiply each digit by each other digit and fill in the numbers in each cell.

36 x 92

       3     6
    +-----+-----+
    | 2 / | 5 / |
9   |  /  |  /  |
    | / 7 | / 4 |
    +-----+-----+
    | 0 / | 1 / |
2   |  /  |  /  |
    | / 6 | / 2 |
    +-----+-----+

添加数字每个对角线。从最低有效数字(在右下方)移动到最高数字(左上角)。

Add the numbers on each diagonal. Move from the least-significant digit (at the lower right) to the most (upper left).

2                                                                    2 (least-significant)
(6 + 1 + 4) = 11 (make this 1, and carry the 1 to the next digit)    1
(5 + 7 + 0 + 1(carried)) = 13 (make this 3, and carry the 1)         3
2 + 1(carried) = 3                                                   3 (most-significant)

答案为3312。

制作二维数字数组。使用单个数字的乘法填充数组。

Make a two-dimensional array of your digits. Fill the array with the multiplications of the single digits together.

写一些逻辑来刮掉对角线,如上所述。

Write some logic to scrape the diagonals as I did above.

这适用于任意大数(只要你还有内存)。

This should work for arbitrarily large numbers (as long as you still have memory left).

这篇关于作业:如何写自己的大数字乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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