C++ 如何存储非常大的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18060242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:41:28  来源:igfitidea点击:

How to store extremely large numbers?

c++data-structuresintlarge-data

提问by Oleksiy

For example I have a factorial program that needs to save really huge integers that can be 50+ digits long. The absolute maximum primitive data type in C++ is unsigned long long intwith a maximum value 18446744073709551615which is only 20 digits long. Here's the link to the limits of C++: http://www.cplusplus.com/reference/climits/

例如,我有一个阶乘程序,它需要保存可能超过 50 位数字的非常大的整数。C++ 中的绝对最大原始数据类型unsigned long long int的最大值18446744073709551615只有 20 位长。这是 C++ 限制的链接:http: //www.cplusplus.com/reference/climits/

How do I store numbers that are larger than that in a variable of some sort?

如何在某种变量中存储大于数字的数字?

回答by

If you already have a boost dependency (which many people these days do), you can use the boost multi-precision library. In fact, it already has an example of a factorial programthat can support output up to 128 bits, though extending it further is pretty trivial.

如果您已经有一个 boost 依赖项(现在很多人都这样做),您可以使用 boost多精度库。事实上,它已经有一个阶乘程序的例子,可以支持高达 128 位的输出,尽管进一步扩展它是非常简单的。

回答by Joe Junior

You'll have to use a bigint or bignum implementation. There are some libraries out there like this: http://gmplib.org/

您必须使用 bigint 或 bignum 实现。有一些像这样的库:http: //gmplib.org/

Some more info and a list of libraries: http://en.wikipedia.org/wiki/Bignum

更多信息和图书馆列表:http: //en.wikipedia.org/wiki/Bignum

回答by A.Najafi

You can use array. First you should copy that giant number array,then use comma after 19 digits:

您可以使用数组。首先你应该复制那个巨大的数字数组,然后在 19 位数字后使用逗号:

unsigned long long int num[]= {
        7316717653133062491,9225119674426574742,3553491949349698352,0,312774506326239578,3180169848018694788,
        5184385861560789112,9494954595017379583,3195285320880551112,5406987471585238630,5071569329096329522,
        7443043557668966489,5044524452316173185,6403098711121722383,1136222989342338030,8135336276614282806,
        4444866452387493035,8907296290491560440,7723907138105158593,0,7960866701724271218,8399879790879227492,
        1901699720888093776,6572733300105336788,1220235421809751254,5405947522435258490,7711670556013604839,
        5864467063244157221,5539753697817977846,1740649551492908625,6932197846862248283,9722413756570560574,
        9026140797296865241,4535100474821663704,8440319989000889524,3450658541227588666,8811642717147992444,
        2928230863465674813,9191231628245861786,6458359124566529476,5456828489128831426,0,7690042242190226710,
        5562632111110937054,4217506941658960408,0,7198403850962455444,3629812309878799272,4428490918884580156,
        1660979191338754992,0,0,5240636899125607176,0,6058861164671094050,7754100225698315520,0,0,
        5593572972571636269,5618826704282524836,0,0,8232575304207529634,50};

回答by Jitendra Rajput

There are many ways to store very big number which are below:

有很多方法可以存储非常大的数字,如下所示:

  1. string
  2. File
  3. Link list
  4. Vector/Dynamic array
  1. 细绳
  2. 文件
  3. 链接列表
  4. 矢量/动态数组

Note: Please don't use array to avoid memory problem issues.

注意:请不要使用数组以避免内存问题。