长数字。分配
世界! 我有一个问题。今天我尝试创建一个代码,它可以找到加泰罗尼亚号码。但在我的程序中可能会有很长的数字。我找到了分子和分母。但我不能除长数!此外,该程序中必须仅使用标准库。请帮助我。 这是我的
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
const int base = 1000*1000*1000;
vector <int> a, b;
int n, carry = 0;
cin>>n;
a.push_back(n);
for (int ii=n+2; ii!=(2*n)+1;++ii) {
carry = 0;
for (size_t i=0; i<a.size() || carry; ++i) {
if (i == a.size())
a.push_back (0);
long long cur = carry + a[i] * 1ll * ii;
a[i] = int (cur % base);
carry = int (cur / base);
}
}
while (a.size() > 1 && a.back() == 0) a.pop_back();
b.push_back(n);
for (int ii=1; ii!=n+1;++ii) {
carry = 0;
for (size_t i=0; i<b.size() || carry; ++i) {
if (i == b.size())
b.push_back (0);
long long cur = carry + b[i] * 1ll * ii;
b[i] = int (cur % base);
carry = int (cur / base);
}
}
while (b.size() > 1 && b.back() == 0) b.pop_back();
cout<<(a.empty() ? 0 : a.back());
for (int i=(int)a.size()-2; i>=0; --i) cout<<(a[i]);
cout<<" ";
cout<<(b.empty() ? 0 : b.back());
for (int i=(int)b.size()-2; i>=0; --i) cout<<(b[i]);
//system("PAUSE");
cout<<endl;
return 0;
}
代码抱歉我的英语不好=)
world!
I have a problem. Today I tried to create a code, which finds Catalan number. But in my program can be long numbers. I found numerator and denominator. But i can't div long numbers! Also, only standard libraries was must use in this program. Help me please.
This is my code
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
const int base = 1000*1000*1000;
vector <int> a, b;
int n, carry = 0;
cin>>n;
a.push_back(n);
for (int ii=n+2; ii!=(2*n)+1;++ii) {
carry = 0;
for (size_t i=0; i<a.size() || carry; ++i) {
if (i == a.size())
a.push_back (0);
long long cur = carry + a[i] * 1ll * ii;
a[i] = int (cur % base);
carry = int (cur / base);
}
}
while (a.size() > 1 && a.back() == 0) a.pop_back();
b.push_back(n);
for (int ii=1; ii!=n+1;++ii) {
carry = 0;
for (size_t i=0; i<b.size() || carry; ++i) {
if (i == b.size())
b.push_back (0);
long long cur = carry + b[i] * 1ll * ii;
b[i] = int (cur % base);
carry = int (cur / base);
}
}
while (b.size() > 1 && b.back() == 0) b.pop_back();
cout<<(a.empty() ? 0 : a.back());
for (int i=(int)a.size()-2; i>=0; --i) cout<<(a[i]);
cout<<" ";
cout<<(b.empty() ? 0 : b.back());
for (int i=(int)b.size()-2; i>=0; --i) cout<<(b[i]);
//system("PAUSE");
cout<<endl;
return 0;
}
P.S. Sorry for my bad english =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必计算 (2n)!为了计算 (2n)!/(n!(n+1)!)),因为您可以使用 此链接:
C(0)=1
C(n)=(4n-2)C(n-1)/(n+1)
仅使用 32 位算术即可得出前 15 项。只需使用任意长度整数与 16 位整数的乘法和除法即可生成最多 16384 个项。这比一般的任意精度算术容易得多,并且很可能被设置为家庭作业。
You don't have to calculate (2n)! in order to calculate (2n)!/(n!(n+1)!)), because you can use the recurrence relation given in this link:
C(0)=1
C(n)=(4n-2)C(n-1)/(n+1)
This gives you the first 15 terms using just 32-bit arithmetic. And you can generate up to 16384 terms just by using multiplication and division of an arbitrary-length integer by a 16-bit integer. This is much easier than general arbitrary-precision arithmetic, and might well be set as a homework task.
在长数的表示中实现除法是非常困难的,但它是真实的。在我看来,最简单的方法是Long_division。
It will be very difficult implement division in your representation of long numbers, but it is real. In my opinion, the simplest method is Long_division.