使用分而治之的二进制到十进制转换 [英] Binary to Decimal Conversion using Divide-and-conquer

查看:79
本文介绍了使用分而治之的二进制到十进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前只能从事一项任务,几乎到处都在寻找甚至暗示我要做什么.

赋值很简单,我们将得到一个以矢量形式的二进制数(例如[1,1,1,1],然后我们将计算该数的十进制形式,然后将其放回相同的向量形式(例如,[1,5]为上一个示例的答案).

起初我以为这将是一个简单的解决方案,但我很快发现我们将使用此方法来计算非常大的数字,例如二进制形式的300 1.

现在,当我意识到尝试直接进行计算的错误后,我很快找到了分而治之"的方法思想,但是我没有找到一个单独的地方来给出如何使用它的精确示例.上下文.

由于这是一项作业,所以我希望提出一个答案,该答案实际上可以解释该概念并提供示例,而不是直接的代码段.

先谢谢您

马修

解决方案

写一个以10为底的数学引擎.

应包括另一个10进制数的加法,以及int的乘法. (加倍就足够了)

遍历二进制数字,并跟踪与该数字相对应的以10为底的数字.

有条件地积累.

唯一困难的部分是基础10数学系统,其他所有内容都需要3到8行代码.

不幸的是,由于二进制数的任何数字都可能影响等效的基数10的任何数字,因此,有效执行此操作的方法非常有限.可能有一些花哨的方法,但是对于300位数,您应该不要打扰.

I am currently stuck on an assignment and have looked nearly everywhere for even a hint at what I am trying to do.

The assignment is simple, we are to be given a binary number in the form of a vector (e.g. [1,1,1,1] and we are to compute the decimal form of this number and put in back into the same vector form (e.g. [1,5] for the answer to the previous example).

While at first I thought this would have an easy solution, I soon found that we are to use this method to calculate extremely large numbers such as 300 1's in binary.

Now after I realized my mistake of trying to straight up calculate it, I soon found the "divide-and-conquer" method idea but I did not find a single place that gave a precise example of how to use it in this context.

Since this is an assignment, I would rather an answer be proposed that actually explains the concept and provides examples rather than a straight up block of code.

Thank you in advance,

Matthew

解决方案

Write a base 10 math engine.

It should include addition by another base 10 number, and multiplication by int. (well doubling is enough)

Iterate over the binary digits, keeping track of a base 10 number that corresponds to the digit.

Conditionally accumulate.

The only hard part is the base 10 math system, everything else takes 3 to 8 lines of code.

Sadly, there is very limited easy ways to do thus more efficiently, as any digit of a binary number can influence any digit of the equivalent base 10 number. There may be fancy ways, but for 300 digits you should not bother.

这篇关于使用分而治之的二进制到十进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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