C + + additive high-precision algorithm

Source: Internet
Author: User

C + + high-precision algorithm, for the novice is still a big challenge, as long as you overcome it, you open a new chapter of programming, algorithm.

I sent this code is not very good, take up a lot of memory and run a long time (not more than 1 seconds), but very good understanding, very suitable for beginners

The essence of the high-precision algorithm is to put the array programming string, and then add the string as a vertical type:

1#include <iostream>2#include <cmath>3#include <cstring>4 using namespacestd;5 intMain ()6 {7     Chara[100010],b[100010]; 8cin>>a>>b;//enter a two string. 9     intc[100010],d[100010],h[100010],n1,n2,i,jw=0;//set three positive-shaped arrays to hold two addend, one for storage and the other. N1n2 to store the length of the string A, B. TenN1=strlen (a);//Save the length of string a into N1 OneN2=strlen (b); A      for(i=0; i<n1/2; i++) Swap (a[i],a[n1-1-I.]);//swap the order of the addend strings so that you can align them as if you were adding a vertical type.  -      for(i=0; i<n2/2; i++) Swap (b[i],b[n2-1-I.]);//Ibid . -      for(i=0; i<n1;i++) c[i]=a[i]-'0';//Convert the string C into array A, where-' 0 ' is the way of conversion the      for(i=0; i<n2;i++) d[i]=b[i]-'0'; -     if(N1&GT;N2) swap (N1,N2);//compare two array lengths, and choose a long to do for loop condition.  -      for(i=0; i<n2;i++) h[i]=c[i]+D[i]; -      for(i=0; i<n2;i++) +     { -          if(h[i]>=Ten) +          { Ajw=h[i]/Ten;//Take rounding ath[i]=h[i]-Ten;//to fall into a position -h[i+1]++;  -          }  -          if(jw>0) -          { -jw=0;//Initialize rounding in          } -     } to     if(h[n2]!=0) cout<//determine if the highest bit has a carry, then the output.  +      for(i=n2-1; i>=0; i--) cout<<H[i]; -}
a+b High Accuracy algorithm

C + + additive high-precision algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.