在C ++中处理大型数字输入的最佳方法是什么(例如10^100
)?
对于算法我经常切换到ruby,有时我会使用字符串。
还有其他好方法吗?
答案 0 :(得分:15)
答案 1 :(得分:7)
由Owen Astrachan查看The Large Integer Case Study in C++.pdf。我发现这个文件对于详细介绍和代码实现非常有用。它不使用任何第三方库。我用它来处理大量数字(只要你有足够的内存来存储vector<char>
)没有问题。
<强>观强>:
它通过在vector<char>
中存储big int来实现任意精度整数类。
vector<char> myDigits; // stores all digits of number
然后,所有与big int相关的操作(包括<<, >>, +, -, *, ==, <, !=, >, etc.
)都可以根据此char array
上的操作完成。
代码的味道: 这是头文件,您可以在pdf文件中找到带有代码的cpp。
#include <iostream>
#include <string> // for strings
#include <vector> // for sequence of digits
using namespace std;
class BigInt
{
public:
BigInt(); // default constructor, value = 0
BigInt(int); // assign an integer value
BigInt(const string &); // assign a string
// may need these in alternative implementation
// BigInt(const BigInt &); // copy constructor
// ~BigInt(); // destructor
// const BigInt & operator = (const BigInt &);
// assignment operator
// operators: arithmetic, relational
const BigInt & operator += (const BigInt &);
const BigInt & operator -= (const BigInt &);
const BigInt & operator *= (const BigInt &);
const BigInt & operator *= (int num);
string ToString() const; // convert to string
int ToInt() const; // convert to int
double ToDouble() const; // convert to double
// facilitate operators ==, <, << without friends
bool Equal(const BigInt & rhs) const;
bool LessThan(const BigInt & rhs) const;
void Print(ostream & os) const;
private:
// other helper functions
bool IsNegative() const; // return true iff number is negative
bool IsPositive() const; // return true iff number is positive
int NumDigits() const; // return # digits in number
int GetDigit(int k) const;
void AddSigDigit(int value);
void ChangeDigit(int k, int value);
void Normalize();
// private state/instance variables
enum Sign{positive,negative};
Sign mySign; // is number positive or negative
vector<char> myDigits; // stores all digits of number
int myNumDigits; // stores # of digits of number
};
// free functions
ostream & operator <<(ostream &, const BigInt &);
istream & operator >>(istream &, BigInt &);
BigInt operator +(const BigInt & lhs, const BigInt & rhs);
BigInt operator -(const BigInt & lhs, const BigInt & rhs);
BigInt operator *(const BigInt & lhs, const BigInt & rhs);
BigInt operator *(const BigInt & lhs, int num);
BigInt operator *(int num, const BigInt & rhs);
bool operator == (const BigInt & lhs, const BigInt & rhs);
bool operator < (const BigInt & lhs, const BigInt & rhs);
bool operator != (const BigInt & lhs, const BigInt & rhs);
bool operator > (const BigInt & lhs, const BigInt & rhs);
bool operator >= (const BigInt & lhs, const BigInt & rhs);
bool operator <= (const BigInt & lhs, const BigInt & rhs);
答案 2 :(得分:6)
您是否正在寻找如何对您收到的大型输入进行操作?有一个big integer C++库(类似于Java),允许您执行算术运算......
答案 3 :(得分:5)
如果您希望为此目的制作自己的代码,请尝试使用字符串存储大数字...然后您可以在它们上创建基本操作,例如+ - / * ...例如 -
#include <iostream>
using namespace std;
string add (string &s1, string &s2){
int carry=0,sum,i;
string min=s1,
max=s2,
result = "";
if (s1.length()>s2.length()){
max = s1;
min = s2;
} else {
max = s2;
min = s1;
}
for (i = min.length()-1; i>=0; i--){
sum = min[i] + max[i + max.length() - min.length()] + carry - 2*'0';
carry = sum/10;
sum %=10;
result = (char)(sum + '0') + result;
}
i = max.length() - min.length()-1;
while (i>=0){
sum = max[i] + carry - '0';
carry = sum/10;
sum%=10;
result = (char)(sum + '0') + result;
i--;
}
if (carry!=0){
result = (char)(carry + '0') + result;
}
return result;
}
int main (){
string a,b;
cin >> a >> b;
cout << add (a,b)<<endl;
return 0;
}
答案 4 :(得分:3)
假设您正在谈论输入数字,双精度会让您达到1.7976931348623157 x 10 ^ 308
答案 5 :(得分:3)
您可能希望查看gmplib,这是C和C ++的任意精确数字处理库
答案 6 :(得分:3)
如果你想要它准确,你需要一个库来处理大数字。 Java有BigInt,无论你想要多少位数,它总是准确的,并提供它们的数学运算。包含所有源代码,你可以传输它,但这确实不是C ++最擅长的事情 - 我使用基于JVM的语言并使用其中一个Big库。
我不认为我会使用ruby,除非你想要它很慢,而且我假设因为你在谈论C ++,速度在某种程度上是设计考虑因素。
答案 7 :(得分:2)
正如其他人已经指出的那样,C ++中有各种各样的bignum /任意精度库,你可能会发现它们很有用。如果不需要速度,我的印象是Python和Lisp都默认使用bignums。
答案 8 :(得分:0)
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
int main()
{
using namespace boost::multiprecision;
cpp_int u = 1;
for(unsigned i = 1; i <= 100; ++i)
u *= i;
// prints 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 (i.e. 100!)
std::cout << u << std::endl;
return 0;
}
答案 9 :(得分:-2)
我认为进行此类算术运算的最佳方法是使用字符串。将输入作为命令行参数提供,然后使用atoi()
和itoa()
等字符串函数操作整个逻辑!但是,嘿,这可以用于乘法和除法吗?我认为这样输入的strlen
字符串对编译器编程无关紧要,直到逻辑正常。