c++ 中的大整数
我知道这个问题可能已经在这个论坛和网络上被问过很多次了。我被要求在 C++ 中创建一个大整数的实现,但是有一个限制,即我的构造函数之一应该采用 int 作为参数......所以我猜测会有多个非默认构造函数。所以我的问题是,最简单的方法是什么?
I know this question has probably been asked in this forum many times and in the web as well. I am asked to create an implementation of a big integer in c++, however there is a constraint that one of my constructor should take an int as an argument... so I am guessing there will be more than one non-default constructor... so my question is, what would be the easiest way to do this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么,问题似乎是“如何将整数转换为位列表”?换句话说,整数的以 2 为底的表示形式是什么?
由于这应该是家庭作业,所以让我通过以 10 为基数的思考来讨论这个问题;经过一番思考,适当的改变应该是显而易见的。
给定一个以 10 为基数的数字,很容易算出最右边的数字是什么:它只是除以 10 时的余数。例如,如果 n=1234,那么它的最右边的数字是 n%10 = 4。要获取下一个最右边的数字,我们除以 10(得到 123),然后重复该过程。所以:
现在我们已经得到了答案[4,3,2,1]。如果我们反转它们,我们就会得到数字的基数 10 的数字:[1, 2, 3, 4]。
The question, then, seems to be "how do I turn an integer into a list of bits"? Put another way, what's the base-2 representation of an integer?
As this is supposed to be homework, let me talk around the problem by thinking in base-10; the appropriate changes should be obvious with some thought.
Given a base 10 number, it's pretty easy to figure out what the rightmost digit is: It's just the remainder when dividing by 10. E.g. if n=1234, then it's rightmost digit is n%10 = 4. To get the next rightmost digit, we divide by 10 (getting 123), and repeat the process. So:
So now we've gotten the answers [4,3,2,1]. If we reverse them, we have the base-10 digits of our number: [1, 2, 3, 4].
C++ BigInt 类
C++ 大整数库
例如编写 big int :
C++ BigInt class
C++ Big Integer Library
to write big int for example :
为什么要重新发明轮子?使用GNU MP 库。
[编辑] 闻起来像家庭作业。因此,当您有一个
BigBit
类时,请执行以下操作:int
参数上的所有位!= 0
,设置BigBit
向量中的位。Why reinvent the wheel? Use the GNU MP library.
[EDIT] Smells like homework. So when you have a
BigBit
class, then do this:int
argument of the constructorint
argument which is!= 0
, set the bit in theBigBit
vector.