在C++中处理大型数字输入的最佳方法是什么(例如10^100
)?
对于算法,我通常切换到ruby,有时我会使用字符串.
还有其他好方法吗?
听起来你正在寻找一种输入任意精度数字的方法.这里有两个你可以使用的库:GMP和MAPM
查看C++中的大整数案例研究.pdf by Owen Astrachan.我发现这个文件对于详细介绍和代码实现非常有用.它不使用任何第三方库.我用它来处理大量数据(只要你有足够的内存来存储vector
)没有问题.
想法:它通过在a中存储big int来实现任意精度整数类vector
.
vectormyDigits; // stores all digits of number
然后,所有与big int相关的操作(包括<<, >>, +, -, *, ==, <, !=, >, etc.
)都可以基于此操作完成char array
.
代码的味道:这是头文件,您可以在pdf文件中找到带有代码的cpp.
#include#include // for strings #include // 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 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);
您在寻找如何对您收到的大型输入执行操作吗?有一个大整数C++库(类似于Java),允许您执行算术运算......
如果您希望为此编写自己的代码,请尝试使用字符串存储大数字...然后,您可以在其上创建+-/ *之类的基本操作,例如-
#includeusing 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)<