Algorithm notes-----Large integers-+------Array---efficiency

Source: Internet
Author: User

1 //2 //Created by Alim on 2017/12/23.3 //4 5 6#include <stdlib.h>7#include <cstring>8#include <iostream>9 using namespacestd;Ten  One #defineM 100 A  - Charsa[ +]; - Charsb[ +]; the  -typedefstruct_node{ -     intS[M];//Digital -     intL//Digital Length +}node,*Pnode; -  +  A voidDzs_add (pnode pa,pnode pb,pnode result) { at     intI, CC; - Pnode temp; -  -     if(Pa->l < pb->l) { -temp =PB; -PB =PA; inPA =temp; -     } to  +      for(intj =0; J < pa->l; ++j) { -RESULT-&GT;S[J] = pa->S[j]; the     } *Result->l = pa->l; $ Panax NotoginsengCC =0; -      for(i =0; I < pb->l; ++i) { theResult->s[i] = (Pa->s[i] + pb->s[i] + cc)%Ten; +CC = (Pa->s[i] + pb->s[i])/Ten; A     } the     if(CC! =0) { +Result->s[i] = result->s[i]+cc; -     } $     if(I >= result->l) { $Result->l = i+1; -     } -  the     return; - }Wuyi  the voidDzs_jian (pnode pa,pnode pb,pnode result) { -     intI, CC; Wu Pnode temp; -  About     if(Pa->l < pb->l) { $temp =PB; -PB =PA; -PA =temp; -     } A      for(intj =0; J < pa->l; ++j) { +RESULT-&GT;S[J] = pa->S[j]; the     } -Result->l = pa->l; $CC =0; the      for(i =0; I < pb->l; ++i) { the         if(Pa->s[i] >= pb->S[i]) { theResult->s[i] = pa->s[i]-pb->S[i]; the}Else { -Result->s[i +1] -=1; inResult->s[i] = Pa->s[i] +Ten-Pb->S[i]; the         } the     } About     if(I >= result->l) { theResult->l = i+1; the     } the } + intMain () { - Node ans,a,b; thecout <<"Please enter a large integer a:"<<Endl;BayiCIN >>sa; thecout <<"Please enter a large integer b:"<<Endl; theCIN >>sb; -A.L = strlen (SA);//processing with a string, calculating the length -B.L =strlen (SB); the  the     //Backward Storage the     intz=0, I; the      for(inti = a.l-1; I >=0; i--) { -a.s[z++] = sa[i]-'0'; the     } the  thez=0;94      for(inti = b.l-1; I >=0; i--) { theb.s[z++] = sb[i]-'0'; the     } the //Dzs_add (&a, &b, &ans);98Dzs_jian (&a, &b, &ans); Aboutcout <<"The end result is: \ n"; -      for(i = ans.l-1; I >=0; i--) {101cout<<Ans.s[i];102     }103 104cout <<Endl; the     return 0;106}

Algorithm notes-----Large integers-+------Array---efficiency

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.