C + + high-precision algorithm, for beginners is still a big challenge, as long as overcome it, you opened a new chapter of programming, algorithm.
The code I sent is not very good, it takes up a lot of memory and runs for a long time (no more than 1 seconds), but it's very well understood.
The essence of the high precision algorithm is to use the array programming string, and then add the string like a vertical:
A+b high precision algorithm #include <iostream> #include <cmath> #include <cstring> usi
NG namespace Std;
int main () {char a[100010],b[100010];
cin>>a>>b;//enter two strings. The int c[100010],d[100010],h[100010],n1,n2,i,jw=0;//is provided with three positive shaping arrays to store two addends, one for storage and the other.
N1n2 to store the length of the string A and B.
N1=strlen (a);//To save the length of string A to N1 n2=strlen (b);
for (i=0;i<n1/2;i++) swap (a[i],a[n1-1-i]),//swap the order of the addends string, so that you can align, just like an addition vertical.
for (i=0;i<n2/2;i++) swap (b[i],b[n2-1-i]);/ditto for (i=0;i<n1;i++) c[i]=a[i]-' 0 ';//convert string C to array a, where-' 0 ' is the conversion method
for (i=0;i<n2;i++) d[i]=b[i]-' 0 '; if (n1>n2) swap (N1,N2);
Compare the length of two arrays and select the long conditions for the for loop.
for (i=0;i<n2;i++) h[i]=c[i]+d[i]; for (i=0;i<n2;i++) {if (h[i]>=10) {JW=H[I]/10;//Fetch carry h[i]=h[i]-10;//to fall into position h[i+1]++
;
The IF (jw>0) {jw=0;//initialization carry}} if (h[n2]!=0) cout<
The above is a small series for everyone to bring the C + + high precision algorithm to achieve the full content of the simple, hope to help you, a lot of support cloud Habitat Community ~