Simple implementation of C + + high precision algorithm _c language

Source: Internet
Author: User
Tags arrays

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 ~

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.