High precision (add, subtract, multiply, divide) algorithm

Article directory

High precision algorithm belongs to the mathematical calculation method of dealing with large numbers. In general scientific calculation, it will often be hundreds of decimal places or more, of course, it may be hundreds of billions of large numbers. Generally, these numbers are collectively called high-precision numbers. High-precision algorithm is a kind of analog addition, subtraction, multiplication, division, multiplier, factorial, square root and other operations for super large data by computer.

Usually we use array to store every bit of high precision number.
In the following operations, we put the high digit in A[N], and the low digit in A[0]
For example, 1234, A[0] put 4, A[1] put 3, A[2] put 2, A[3] put 1

High precision addition

(C=A+B) both A and B are of high precision

791. High precision addition

Given two positive integers, sum them.

Input format
There are two lines, each containing an integer.

Output format
A total of one line containing the sum of the requirements.

Data range
1 ≤ integer length ≤ 100000
Input example:
12
23
Output example:
35

#include<iostream>
#include<vector>

using namespace std;
//C=A+B
vector<int> add(vector<int> &A,vector<int> &B)
{
	vector<int> C;
	int t=0; //t is carry.
	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];
		C.push_back(t%10);
		t/=10;
	} 
	if(t) C.push_back(1);
	return C;
}

int main()
{
	string a,b;
	vector<int> A,B;
	
	cin>>a>>b;//a="123456"
	for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//A=[6,5,4,3,2,1]
	for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');
	
	vector<int> C=add(A,B);
	
	for(int i=C.size()-1;i>=0;i--) cout<<C[i];
	return 0; 
}

High precision subtraction

(C=A-B) both A and B are of high precision

792. high precision subtraction
Given two positive integers, calculate their difference, and the result may be negative.

Input format
There are two lines, each containing an integer.

Output format
A total of one line, including the difference.

Data range
1 ≤ integer length ≤ 105
Input example:
32
11
Output example:
21

#include<iostream>
#include<vector>

using namespace std;
//Judge whether there is a > = b 
bool cmp(vector<int> &A,vector<int> &B)
{
	if(A.size()!=B.size()) return A.size()>B.size();
	for(int i=A.size()-1;i>=0;i--)
	  if(A[i]!=B[i])
	    return A[i]>B[i];
	return true;       
}

//C=A-B
vector<int> sub(vector<int> &A,vector<int> &B)
{
	vector<int> C;
	for(int i=0,t=0;i<A.size();i++)
	{
		t=A[i]-t;
		if(i<B.size()) t-=B[i];
		C.push_back((t+10)%10);//If t < 0, borrow + 10, if > = 0, add 10 + 10 or t
		if(t<0) t=1; //Borrow position 
		else t=0; 		
	}
	while(C.size()>1 && C.back()==0) C.pop_back();
	//Remove redundant 0, e.g. 125-123 = 2, prevent output 002 
	return C;
}

int main()
{
	string a,b;
	vector<int> A,B;
	
	cin>>a>>b;//a="123456"
	for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//A=[6,5,4,3,2,1]
    for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');
    
    if(cmp(A,B))
    {
    	vector<int> C=sub(A,B);
    	for(int i=C.size()-1;i>=0;i--) cout<<C[i];
	}
	else       //When a < B, use B-A and add a minus sign in front 
	{
		vector<int> C=sub(B,A); 
		cout<<"-";
		for(int i=C.size()-1;i>=0;i--) cout<<C[i]; 
	}
	return 0;
}

High precision multiplication

(C=A*b) A is high precision, B is low precision (small number of digits, int can be used)

793. High precision multiplication
Given two positive integers A and B, please calculate the value of A * B.

Input format
There are two lines in total. The first line contains the integer A, and the second line contains the integer B.

Output format
A total of one line, containing the value of A * B.

Data range
1 ≤ A length ≤ 100000
1≤B≤10000
Input example:
2
3
Output example:
6

#include<iostream>
#include<vector>
using namespace std;

//C=A*b
vector<int> mul(vector<int> &A,int b)
{
	vector<int> C;
	int t=0;//carry
	for(int i=0;i<A.size() || t;i++)
	{
		if(i<A.size()) t+=A[i]*b;//Multiply b by A[i], not one of b by A[i] 
		C.push_back(t%10);  //Only the last digit after multiplication 
		t/=10;    
	} 
	return C;
}

int main()
{
	string a;
	int b;
	cin>>a>>b;//a="123456"
	vector<int> A;
	
	for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//A=[6,5,4,3,2,1]
	
	vector<int> C=mul(A,b);
	
	for(int i=C.size()-1;i>=0;i--) cout<<C[i];
	
	return 0;
}

High precision Division

(C=A/b) a is high precision, B is low precision

794. High precision Division
Given two positive integers A and B, please calculate the quotient and remainder of A / B.

Input format
There are two lines in total. The first line contains the integer A, and the second line contains the integer B.

Output format
There are two lines in total. The first line outputs the quotient and the second line outputs the remainder.

Data range
1 ≤ A length ≤ 100000
1≤B≤10000
Input example:
7
2
Output example:
3
1

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
//A/b, quotient C, remainder r 
vector<int> div(vector<int> &A,int b,int &r) 
{
	vector<int> C;
	r=0;
	for(int i=A.size()-1;i>=0;i--)
	{
		r=r*10+A[i];
		C.push_back(r/b);
		r%=b;
	}
	reverse(C.begin(),C.end());//To invert, the header file "include < algorithm > is required, for example, 12345 becomes 54321 
	while(C.size()>1 && C.back()==0) C.pop_back();
	 //Remove the extra 0. For example, 89 / 9 will get 9 over 8 instead of 09 over 8 
	return C;
}

int main()
{
	string a;
	int b;
	cin>>a>>b;//a="123456"
	
	vector<int> A;
	for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//A=[6,5,4,3,2,1]
    
	int r;//Remainder 
    vector<int> C=div(A,b,r);
    
    for(int i=C.size()-1;i>=0;i--) cout<<C[i];
    cout<<endl<<r<<endl;

    return 0;  
}   
 
40 original articles published, 98 praised, 10000 visitors+
Private letter follow

Posted by jesushax on Tue, 11 Feb 2020 06:01:12 -0800