小朋友的球,我编写的代码在本地一直提示[Error] expected unqualified-id before '__int128'
,一直调不出来,请大家赐教(
#include <iostream>
using namespace std;
__int128 dp[105][105];
void write(__int128 x)
{
int ans[1005] = {0}, top = 0;
do{
ans[top++] = x % 10;
x /= 10;
} while(x);
while(top)
{
putchar(ans[--top] + '0');
}
}
int main()
{
int n, m;
for(int i = 1; i <= 100; i++)
{
for(int j = 1; j <= 100; j++)
{
if(j == 1) dp[i][j] = 1;
else
{
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] * j;
}
}
}
while(cin >> n >> m)
{
write(dp[n][m]);
}
return 0;
}
这个要看是什么编译器,不是所有的都支持
如果支持,可以加上
#include <bits/stdc++.h>
不知道你这个问题是否已经解决, 如果还没有解决的话:可能是编译器不支持__int128的数据类型,无法进行编译。解决方法是改用其他精度更低的数据类型,例如long long或者自定义高精度类型。如果使用自定义高精度类型,可以通过实现类似于加减乘除等操作的函数来实现各种计算。以下是一个简单的自定义高精度类BigInt的示例代码:
class BigInt {
private: vector digits;
public: BigInt(int x = 0) { while(x) { digits.push_back(x % 10); x /= 10; } if(digits.empty()) digits.push_back(0); }
BigInt(string s) {
for(int i = s.length() - 1; i >= 0; i--)
digits.push_back(s[i] - '0');
if(digits.empty()) digits.push_back(0);
}
int size() const {
return digits.size();
}
int operator [] (int i) const {
return digits[i];
}
int& operator [] (int i) {
return digits[i];
}
friend ostream& operator << (ostream& out, const BigInt& x) {
for(int i = x.digits.size() - 1; i >= 0; i--)
out << x.digits[i];
return out;
}
friend BigInt operator + (const BigInt& x, const BigInt& y) {
int carry = 0;
BigInt z;
for(int i = 0; i < max(x.size(), y.size()); i++) {
int sum = carry + x[i] + y[i];
z.digits.push_back(sum % 10);
carry = sum / 10;
}
if(carry) z.digits.push_back(carry);
return z;
}
friend BigInt operator - (const BigInt& x, const BigInt& y) {
int borrow = 0;
BigInt z;
for(int i = 0; i < x.size(); i++) {
int diff = x[i] - borrow - (i < y.size() ? y[i] : 0);
if(diff < 0) {
borrow = 1;
diff += 10;
} else {
borrow = 0;
}
z.digits.push_back(diff);
}
while(z.size() > 1 && z[z.size() - 1] == 0) z.digits.pop_back();
return z;
}
friend BigInt operator * (const BigInt& x, const BigInt& y) {
BigInt z;
z.digits.resize(x.size() + y.size());
for(int i = 0; i < x.size(); i++)
for(int j = 0; j < y.size(); j++) {
z[i + j] += x[i] * y[j];
if(z[i + j] >= 10) {
z[i + j + 1] += z[i + j] / 10;
z[i + j] %= 10;
}
}
while(z.size() > 1 && z[z.size() - 1] == 0) z.digits.pop_back();
return z;
}
friend BigInt operator / (const BigInt& x, const BigInt& y) {
BigInt z;
BigInt curr;
for(int i = x.size() - 1; i >= 0; i--) {
curr.digits.insert(curr.digits.begin(), x[i]);
int left = 0, right = 9;
while(left < right) {
int mid = left + right + 1 >> 1;
if(y * mid <= curr) left = mid;
else right = mid - 1;
}
curr = curr - y * left;
z.digits.insert(z.digits.begin(), left);
}
while(z.size() > 1 && z[z.size() - 1] == 0) z.digits.pop_back();
return z;
}
};
使用示例:
BigInt x = "123456789012345678901234567890"; BigInt y = "987654321098765432109876543210"; BigInt z = x * y; cout << z << endl; // 输出 1219326311370217951198846273036682459791746835727852270449027210