((C ++)存储int值10 ^ 80 [重复]

问题描述 投票:0回答:1

我有一个需要使用变量的问题

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 broj = N;

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

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

cout << Z;

return 0;
}
c++ variables int unsigned bigint
1个回答
0
投票
我认为您应该使用大整数。有几种方法。首先,使用C-具有C ++接口的库。 GNU多精度算术库:http://gmplib.org/第二种方法-您应该实现自己的BigInteger类。

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

换句话说,您只是将大数字分开,并将每个部分记录到向量中。
© www.soinside.com 2019 - 2024. All rights reserved.