High-precision multiplication

Source: Internet
Author: User

Question J: "High precision" high accuracy multiplication time limit: 1 Sec memory limit: up to MB
Submitted by: Solution: 17
Submitted State [Discussion Version] The title describes the third lock on the door, the need to use high-precision multiplication to decipher the corresponding password, of course, the "universal algorithm" must be used to calculate two non-negative integers a, b of the product, where A and b the number of bits within 10,000 bits. Input a total of two rows of data, the first behavior of a nonnegative integer A, the second behavior a nonnegative integer b,a, B, the number of bits are within 10000. Output outputs a number, which is the product of A and B. Sample input
23
Sample output
6

Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[10001];//store A, from low to highCharb[10001];//store A, from low to highCharc1[10001];//Temporary storageCharc2[10001];//Temporary storageintf[20000]={0};//store results in reverse ordervoidChengintLaintlb) {    intk=0; intGE; intShi; if(la>=lb) {         for(intI=0; i<lb;i++){             for(intj=0; j<la;j++) {f[k+j]+= (a[j]-'0') * (b[i]-'0'); } k++; }         for(intL=0; L<= (la+lb-1); l++){            if(f[l]>=Ten) {GE=f[l]%Ten; Shi=f[l]/Ten; F[L]=GE; F[l+1]+=Shi; }        }    }    if(la<lb) {         for(intI=0; i<la;i++){             for(intj=0; j<lb;j++) {f[k+j]+= (b[j]-'0') * (a[i]-'0'); } k++; }         for(intL=0; L<= (lb+la-1); l++){            if(f[l]>=Ten) {GE=f[l]%Ten; Shi=f[l]/Ten; F[L]=GE; F[l+1]+=Shi; }        }    }}voidPrintintl) {    intk=0;  for(intj=l;j>=0; j--){        if(f[j]==0) {k++; }Else{             Break; }    }    if(k==l+1) {printf ("0"); }Else{         for(inti=l-k;i>=0; i--) {printf ("%d", F[i]); }    }}intMain () {intLA; intlb;  while(SCANF ("%s%s", C1,C2)! =EOF) {La=strlen (C1);  for(intI=0; i<la;i++) {A[i]=c1[la-1-i]; } lb=strlen (C2);  for(intI=0; i<lb;i++) {B[i]=c2[lb-1-i];        } cheng (la,lb); Print (La+lb-1); printf ("\ n"); Memset (F,0,20000*sizeof(int)); }    return 0;}





High-precision 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.