【问题标题】:(C++) Storing an int value of 10^80 [duplicate](C ++)存储10 ^ 80的int值[重复]
【发布时间】:2020-03-26 05:57:04
【问题描述】:

我有一个需要处理变量的问题

1

其中一个测试用例适用于一个值:

3141592653589793238462643383279502884197169399375

并使用 unsigned long long int,程序将此值的最大值视为:

18446744073709551615

显然,我需要存储一个更大的值。

我该如何解决这个问题?

#include <iostream>

using namespace std;

int main()
{

unsigned long long int N;
cin >> N;
unsigned long long int Z;
int result = 0;

unsigned long long int num = N;

while (N > 0) {
    Z += N % 10;
    N /= 10;
}

while (Z % 9 != 0) {
    Z += num;
    result++;
}

cout << Z;

return 0;
}

【问题讨论】:

标签: c++ variables int unsigned bigint


【解决方案1】:

在我看来你应该使用大整数。有几种方法。 首先,使用具有 C++ 接口的 C - Library。 GNU 多精度算术库:http://gmplib.org/ 第二种方式 - 你应该实现自己的 BigInteger 类。

    template<class Type>
    class BigInt
    {
        typedef typename Type BT;
     protected: 
        std::vector<Type> value_;
    };

换句话说,您只需拆分大数并将每个部分记录到向量中。

【讨论】:

  • 有没有更简单的方法来解决这个问题?
  • @N.T.我觉得不行。 CPU 以字节码解释您的输入,例如 16 - 它是 10000 或 0xf。对于“int”,我们只有 4 个字节,因此我们应该拆分我们的大数并将其记录到数组中。 BigInteger 的所有 Interpate 看起来都是这样。
猜你喜欢
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
相关资源
最近更新 更多