I recently opened a CSDN blog. On the one hand, I want to learn from the resources of CSDN, and on the other hand, I want to use this blog to record the road.
Let's talk about high precision:
High precision is also called a large integer, which is a number beyond the data range of integer (int) or even (longlong). (The range of int is -2147483648, the range of 2147483647/long long is -922 3372 0368 5477 5808 ~ 922 3372 0368 5477 5807)
There is an idea to turn large numbers into string input.
A+B Problem (High Precision)
#include<bits/stdc++.h>
using namespace std;
#define ma 100000
string x,y;
int a[ma],b[ma],c[ma],la,lb,lc;
int main ()
{
cin >>x>>y;
la= x.length();
lb= y.length();
for (int i=0; i<la; i++)
a[la-i]=x[i]-'0';
for (int j=0; j<lb; j++)
b[lb-j]=y[j]-'0';
lc=max(la,lb);
for (int k=1; k<=lc; k++)
{
c[k]+=a[k]+b[k];
c[k+1]=c[k]/10;
c[k]%=10;
}
if (c[lc+1]>0)
lc++;
for (int i=lc; i>0; i--)
cout << c[i];
return 0;
}
length() means the length of the
string string a;
int main ()
{ int n; cin >>a;//input asc n = a.length(); cout <<n;//output 3 return 0; } Subtraction is similar to addition. A*B Problem
#include<bits/stdc++.h>
using namespace std;
#define ma 100000
string x,y;
int a[ma],b[ma],c[ma],la,lb,lc;
int main ()
{
cin >> x>>y;
la = x.length ();
lb = y.length ();
for (int i=0; i<la; i++)
a[la-i]=x[i]-'0';
for (int i=0; i<lb; i++)
b[lb-i]=y[i]-'0';
lc = la+lb;
for (int i=1; i<=la; i++)
{
for (int j=1; j<=lb; j++)
{
c[i+j-1]+=a[i]*b[j];
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
}
}
while (c[lc]==0&&lc >1)lc--;
for (int i=lc; i>0; i--)
cout <<c[i];
return 0;
}
c[i+j-1]+=a[i]*b[j];
Every time you multiply, you have to go one bit to the first
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
there is a carry, and 1 cut 10 ;