Large number plus minus multiplication

Source: Internet
Author: User

The calculation problem of large numbers has always been the subject of the programming competition, and it has been done again before the Blue Bridge Cup competition. Large number of division is more difficult, has not yet to try to achieve, and then have the opportunity to continue to complement the whole good.
algorithm Analysis: three kinds of methods are similar, are bitwise operation, dynamic storage. Once the input data has been processed, it is easy to get an answer for each bit by operation.
Large number of additions

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define MAX 1010using namespace STD;stringb;intA[max],b[max];intSum[max];intT,L1,L2;intMain () {memsetA0,sizeof(a));memset(b,0,sizeof(b));memset(Sum,0,sizeof(sum));Cin>>A;Cin>>B;BOOLflag=true;    L1=a.length (); L2=b.length (); for(inti = l1-1; I >=0; i--) a[l1-1-i]=a[i]-' 0 '; for(inti = l2-1; I >=0; i--) b[l2-1-i]=b[i]-' 0 '; for(inti =0; I < Max (L1,L2) +2; i++) {Sum[i]+=a[i]+b[i];if(Sum[i] >=Ten) {sum[i+1]+=sum[i]/Ten; sum[i]%=Ten; }    } for(inti = max-1; I >=0; i--) {if(Sum[i] = =0&& flag)Continue; flag=false;printf("%d", Sum[i]); }printf("\ n");}

Large number Subtraction

#include <iostream>#include <cstdio>#include <cstring>#define MAXusing namespace STD;CharA[max];CharB[max];CharC[max];intL1,L2;BOOLflag=false;intNum1[max],num2[max];BOOLIf_exchange ();voidExchange ();voidMinus (intx);voidPrint ();intMain () { while(Gets (a) &&gets (b)) {l1=l2=0;memset(NUM1,0,sizeof(NUM1));memset(Num2,0,sizeof(num2));memsetC0,sizeof(c)); Flag=if_exchange (); for(inti =strlen(a)-1; I >=0; i--) {num1[l1]=a[i]-' 0 ';        l1++; } for(inti =strlen(b)-1; I >=0; i--) {num2[l2]=b[i]-' 0 ';        l2++; } for(inti =0; i < L1;        i++) minus (i);    Print (); }return 0;}BOOLIf_exchange () {if(strlen(a) <strlen(b)) {Exchange ();return true; }Else if(strlen(a) = =strlen(b)) { for(inti =0; I < (int)strlen(a); i++) {if(A[i] > B[i]) Break;Else if(A[i] < b[i]) {Exchange ();return true; }Else                Continue; }    }return false;}voidExchange () {strcpy(C,a);strcpy(A, B);strcpy(B,C);return;}voidMinus (intx) {if(Num1[x] >= num2[x]) num1[x]-=num2[x];Else{num1[x+1]--; num1[x]=num1[x]+Ten-NUM2[X]; }return;}voidPrint () {intIBOOLsflag=true;if(flag)cout<<'-'; for(i = l1-1; I >=0; i--) {if(Sflag && num1[i]==0)Continue;Else{sflag=false;printf("%d", Num1[i]); }    }if(Sflag)cout<<0;cout<<endl;return;}

Large number multiplication

#include <iostream>#include <cstdio>#include <cstring>#define MAXusing namespace STD;CharA[max];intL1,L2;intLengthintNum1[max],num2[max];intsave[2*max];voidTime (intx);voidPrint ();intMain () {l1=l2=0; Length=0;memset(NUM1,0,sizeof(NUM1));memset(Num2,0,sizeof(num2));memset(Save,0,sizeof(save)); Gets (a); for(inti =strlen(a)-1; I >=0; i--) {num1[l1]=a[i]-' 0 ';    l1++; } gets (a); for(inti =strlen(a)-1; I >=0; i--) {num2[l2]=a[i]-' 0 ';    l2++; } for(inti =0; i < L1;    i++) time (i); Print ();cout<<endl;return 0;}voidTime (intx) {intASS=NUM1[X];intTem[max];intlmax=0;memset(TEM,0,sizeof(TEM)); for(inti =0; I < L2; i++) {intMid=ass*num2[i];        Tem[i]+=mid; tem[i+1]+=tem[i]/Ten; tem[i]%=Ten; lmax=i+1;if(tem[i+1]!=0) lmax++; } for(inti = x; i < X+lmax;        i++) {save[i]+=tem[i-x]; save[i+1]+=save[i]/Ten; save[i]%=Ten; Length=i;if(save[i+1]!=0) length++; }return;}voidPrint () {BOOLflag=true;intI for(i = length; I >=0; i--) {if(Flag && save[i]==0)Continue;Else{flag=false;printf("%d", Save[i]); }    }if(I <0&& flag = =true)cout<<0;return;}

Large number plus minus multiplication

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.