c ++中的大整数 [英] big integers in c++

查看:183
本文介绍了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??

推荐答案

如何将整数转换为位列表?换句话说,一个整数的基数2表示是什么?

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?

由于这应该是家庭作业,让我通过在base-10中思考问题来讨论这个问题;

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.

给定一个基数为10的数字,很容易知道最右边的数字是什么:它只是除以10如果n = 1234,那么它的最右边的数字是n%10 = 4.为了得到下一个最右边的数字,我们除以10(得到123),并重复这个过程。所以:

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:

1234/10=123; 1234%10 = 4
123/10=12  ; 123%10 = 3
12/10=1    ; 12%10 = 2
1/10=0     ; 1%10 = 1

现在我们已经得到了答案[4,3,2,1 ]。如果我们撤消它们,我们有我们的数字的基数10位:[1,2,3,4]。

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 ++中的大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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