(C++) 存储 10^80 的 int 值

(C++) Storing an int value of 10^80

我在需要使用变量时遇到问题

1 <= N <= 10^80

其中一个测试用例使用一个值:

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++ 接口的 C - Library。 GNU 多精度算术库:http://gmplib.org/ 第二种方式——你应该实现你自己的 BigInteger Class.

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

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