C + + high precision algorithm

High precision addition

Add bit by bit. Pay attention to the storage and carry. After adding the small number, add the rest of the large number. Pay attention that the leading zero sum is equal to 0

// High precision addition 
#include<iostream>
#include<string>
using namespace std;
int res[10000001];//Array of record results 
string a,b;
long long k,r,i,j;
bool flag;
int main(){
	cin>>a>>b;
	if(a.size()<b.size()||a.size()==b.size()&&a<b){//Put the large string in front for easy operation 
		swap(a,b);
	}
	for(i = a.size() - 1,j = b.size()-1;j>=0;i--,j--){//Add their common bits from the low bits
		res[k++] = (r+a[i]-'0'+b[j]-'0')%10;//Add the numbers of the same bits and take one bit
		r = (r+a[i]-'0'+b[j]-'0') /10;//Record carry 
	}
	while(i>=0){//Then add the non operational parts of the large number 
		 res[k++] = (r+a[i]-'0')%10;//Take a bit after adding the number of bits and carry
		 r = (r+a[i]-'0')/10;//Record carry
		 i--; 
	}// 
	if(r){
		res[k++] = r;//If there is a carry, go to the highest position 
	}
	for(i = k-1;i>=0;i--){//output 
		if(res[i]!=0||flag){//Prevent operation of leading 0 output 
			cout<<res[i];
			flag = true;
		} 
	} 
	if(!flag){//If both are 0, the result is 0 
		cout<<0<<endl;
	}
	
	return 0;
}

The above codes are the addition of two positive numbers. If there is a negative number, it needs to be judged separately:

  • If both numbers are negative, remove the negative sign and add a negative sign before the output result.

  • If one of the two numbers is negative, remove the minus sign first and compare the size

If the number with the minus sign removed is greater than another number, you need to subtract another number from the number with the minus sign removed, and then output the minus sign (high-precision subtraction is used at this time)
If the number without minus sign is less than another number, you need to subtract the number without minus sign from another number (high-precision subtraction is used at this time)

High precision subtraction

One by one opposite subtraction, pay attention to the storage of borrowing. After subtracting the small number, reduce the remaining borrowing of the large number, and pay attention that the leading zero and subtraction are equal to 0

// High precision subtraction 
#include<iostream>
#include<string>
using namespace std;
int res[10000001]; //Record results
string a,b; 
long long k = 0,r = 0,i,j;
bool flag;
int main(){
	cin>>a>>b;
	if(a.size()<b.size()||(a.size()==b.size()&&a<b)){//If the first number is smaller than the second number, the result is negative 
		cout<<"-";
		swap(a,b);
	}
	for(i = a.size() - 1,j = b.size() - 1;j >= 0;i--,j--){
		res[k++] = (a[i]-b[j]-r);// Two numbers in the same position are subtracted and then the number of borrowed digits is subtracted
		r=0;//Clear
		if(res[k-1]<0){//If it is negative, borrow one for ten and mark it 
			res[k-1]+= 10;
			r = 1;//Carry of this loan 
		} 
	}
	while(i>=0){// Larger numbers, the remaining bits continue to operate 
		res[k++] = a[i] - '0'- r;//Minus borrowed
		r = 0;//Clear
		if(res[k-1]<0){//If it is negative, borrow one for ten and mark it 
			res[k-1]+= 10;
			r = 1;//Carry of this loan 
		} 
		i--; 
	} 
	for(i = k - 1;i>=0;i--){// Output from back to front 
		if(res[i]!=0 || flag){//Prevent leading 0 
			cout<<res[i];
			flag = true; 
		} 
	}
	if(!flag){
		cout<<0<<endl;//If there is no output, it means that the subtraction result is 0, and 0 is output 
	}
	return 0; 
}

The above code is the subtraction of two positive numbers. If there is a negative number, it needs to be judged separately:

List item

  • If both numbers are negative, remove the minus sign and subtract the last number from the previous one.

  • If one of the two numbers is negative, remove the minus sign first and compare the size

If the first number is negative, add it and output a negative sign (high-precision addition is used at this time)
If the second number is negative, add it (use high-precision addition at this time)

High precision multiplication

I believe everyone has manually calculated multiplication, that is, each bit of one number is multiplied by each bit of another number and finally added. Of course, our program is also simulated in this way
Here is a basic knowledge point. I believe you know it when writing vertical, that is
c[i+j] += a[i]+b[j] (because both a and b start from subscript 0, if starting from subscript 1, c[i+j-1]+=a[i]+b[j])

// High precision multiplication
//Up to 10000000 results
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string a,b;
int k;
int res[10000001];//'3'-'3'+'48'  '6' - 48
int main() {
	cin>>a>>b;
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	for(int i = 0; i < a.size(); i++) {
		for(int j = 0; j<b.size(); j++) {
			res[i+j] += (a[i]-48)*(b[j]-48);//
		}
	}
	for(k = 0; k<=a.size()+b.size(); k++) { // n digits * m digits is equal to n+m digits at most
		res[k] += res[k-1] / 10;//The k bit is equal to the value of this bit plus the carry
		res[k - 1] %= 10;  // k-bit calculation is not necessarily necessary. The value of the previous bit is determined. Just write one bit of this bit directly
	}
	while(!res[k]&&k>=1) {
		k--;//Look for the first bit that is not 0 from back to front
	}
	while(k>=0) { //Output results
		cout<<res[k];
		k--;
	}

	return 0;
}

High precision Division

Manually simulate the division process. Each time a new bit is read, then divide the divisor to get the result of this bit. If the division is not complete, use 0 instead. The remainder of each time is used for the next round of calculation

#include<bits/stdc++.h>
using namespace std;
int res[10000001];
string s;
long long k = 0,a,b,i;
bool flag;
int main(){
	cin>>s>>b;//Enter the divisor, divisor 
	for(i = 0; i < s.size();i++){
		a = a*10+s[i]-'0';//Add the divisor
		res[k++] = a / b;
		a %= b; // Please multiply the remaining round by 10 + and add another digit to continue to multiply 
	} 
	for(i = 0; i < k; i++){//Output results 
		if(res[i]!=0||flag){
			cout<<res[i];
			flag = true;//Prevent leading 0 
		} 
	} 
	if(!flag){
		cout<<0<<endl;//If there is no output, the division result is 0 
	} 
	return 0; 
	
}

The above code is the division of two positive numbers. If there is a negative number, it needs to be judged separately:

  • If both numbers are negative, remove the minus sign, regardless of him.

  • If one of the two numbers is negative, remove the negative sign and add it when outputting.

High precision remainder

This is similar to high-precision division. It doesn't need to record and output quotient

#include<bits/stdc++.h>
using namespace std;
//int res[10000001];
string s;
long long k = 0,a,b,i;
bool flag;
int main(){
	cin>>s>>b;//Enter the divisor, divisor 
	for(i = 0; i < s.size();i++){
		a = a*10+s[i]-'0';//Add the divisor
		//res[k++] = a / b;
		a %= b; // Please multiply the remaining round by 10 + and add another digit to continue to multiply 
	} 
	cout<<a<<endl; 
	
	return 0; 
	
}

Keywords: C++ Algorithm

Added by cheatboy00 on Sat, 25 Sep 2021 14:53:40 +0300