Precision "+" algorithm

A precision "+" algorithm

The preparation of high-precision 1.1 "+", remember the following, the code will be getting

(1) First, we need to understand how big an integer is stored?

(2) followed by storage end, consider how operation?

Second, high-precision "+" core algorithm

2.1 large integer storage

• The large integers each bit is stored into an array, to ensure that high front and this is considered to carry, high up on a number (if this number is easy to end up on the array, but if you fill in on the beginning of an array the number needed to translate an entire array all backwards).

Essence of adding 2.2

• 10 is actually filled into a process: A3A2A1A0 + B2B1B0 = C, consider each corresponding bits Ai + Bi + t (1/0), t is a carry.

2.3 In addition, we want to know

(1) A + BA and B values ​​of less than (10 ^ 6)

(2) AB A and B values ​​of less than (10 ^ 6)

(3) A * a A to satisfy len (A) is less than 10 ^ 6, Note: this is the number of bits

(4) A / a A to satisfy len (A) is less than 10 ^ 6

Third, high-precision "+" code template

Add a comment

// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &A,vector<int> &B)//A与B是倒着表示完的数组
{
    vector<int> C;
    
    int t=0;//进位,一开始是零
    for(int i=0;i<A.size()||i<B.size();i++)
    {
        if(i<A.size()) t+=A[i];
        if(i<B.size()) t+=B[i];//Ai+Bi+t
        C.push_back(t%10);
        t/=10;       
    }
    if(t) C.push_back(1);
    return C;
}
"""
string a, b;
vector<int> A,B;    
cin>>a>>b;
for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//数字字符减去'0'将其转化位整数数字
for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');//大整数存储部分的代码
"""
作者:yxc
链接:https://www.acwing.com/problem/content/793/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin www.cnblogs.com/lastk/p/12442140.html