"Algorithmic Learning Notes" 35. High-precision vertical multiplication SJTU OJ 1274

Source: Internet
Author: User

Description

Enter A, b

The vertical multiplication of the output a*b is shown in the sample format.

Sample INPUT1
119
Sample OUTPUT1
11 9--99
Sample Input2
1010
Sample Output2
    10    10   ---   100
Sample INPUT3
101101
Sample OUTPUT3
        101        101      -----        101      101      -----      10201
Sample Input4
100862
Sample Output4
        2    10086    -----       12      16    2    -----    20172
Sample INPUT5
1234001238412039847102934
Sample OUTPUT5
              12340012384           12039847102934 ------------------------              49360049536             37020037152           111060111456           24680024768         12340012384        86380086688       49360049536      98720099072    111060111456   37020037152  24680024768 12340012384 ------------------------ 148571862351672082734656


Although the problem is not algorithmic difficulty, but one of the inspiration is to make a thorough study of the sample, such as these cases have hidden several conditions.
1. If a number is 1 digits, and the number of digits of the other is >=3, the short one is placed on it, and if 2 or 1 does not need to be processed
2. If only one row in the result is output directly
3. If a row in the result is all 0, do not output

The code is as follows
#include <iostream>#include<cstring>using namespacestd;Chara_input[ -]={0};Charb_input[ -]={0};Charres[ -][ -]={0};//store calculation results for each multiplicationintfinal_res[ -]={0};inlinevoidPrintblank (intNCharFlag =' '){     for(inti =0; I < n; ++i) {cout<<Flag; }    return;}voidCalCharA[],CharB[],intA_len,intB_len) {    intFinal_res_len =0;  for(inti = b_len-1; I >=0; I.)//B[i] is one of the bottom numbers    {        intremain =0;//stores the number of digits to be rounded        intindex = b_len-1I//The results of this multiplication are stored in Res[index]//Reverse Storage        intCur =0;  for(intj = A_len-1; J >=0; --j) {intTmp_res = (a[j]-'0') * (b[i]-'0') +remain; Res[index][cur]= (tmp_res)%Ten+'0'; Remain= Tmp_res/Ten; Cur++; }        if(remain>0)//in the end, one more.Res[index][cur] = remain +'0'; }    //calculate the final result     for(inti =0; i < B_len; ++i) {intremain =0;//the number to be rounded         for(intj =0; J < strlen (Res[i]); ++J)//the lowest I bit remains the same        {            //Final_res J-bit and j-i-1-bit alignment            intTemp_res = Final_res[j+i] + (res[i][j]-'0') +remain; Final_res[j+i] = temp_res%Ten; Remain= Temp_res/Ten; }        if(remain>0)//and the final rounding .{Final_res[strlen (res[i])+i] =remain; if(i==b_len-1) Final_res_len= strlen (Res[i]) +i+1; }Else{            if(i==b_len-1) Final_res_len= strlen (Res[i]) +i; }} printblank (Final_res_len-A_len); cout<<a<<Endl; Printblank (Final_res_len-B_len); cout<<b<<Endl; Printblank (Final_res_len,'-'); cout<<Endl; BOOLTooutput =false;intCot =0;  for(inti =0; i < B_len; ++i)if(b[b_len-1-i]!='0') Cot++; Tooutput= cot>1; if(tooutput) { for(inti =0; i < B_len; ++i)if(b[b_len-1-i]!='0') {Printblank (Final_res_len-Strlen (Res[i])-i);  for(intj = strlen (Res[i])-1; j>=0; --j) cout<<Res[i][j]; cout<<Endl; //Cout<<strlen (res[i]) << ":" <<res[i]<<endl;} printblank (Final_res_len,'-'); cout<<Endl; }     for(inti = final_res_len-1; I >=0; --i) {cout<<Final_res[i]; }cout<<Endl;}intMainintargcChar Const*argv[]) {    Chara[ -]={0}; Charb[ -]={0}; CIN>>A_input; CIN>>B_input; intA_len =0; intB_len =0; //Remove Front 0    BOOLbegan =false;  for(inti =0; I < strlen (a_input); ++i) {if(!began and a_input[i]!='0') began=true; if(began) A[a_len++] =A_input[i]; } began=false;  for(inti =0; I < strlen (b_input); ++i) {if(!began and b_input[i]!='0') began=true; if(began) B[b_len++] =B_input[i]; }        //the above is the initialization of    if(a_len>=3and B_len = =1) Cal (B,a,b_len,a_len); Elsecal (A,b,a_len,b_len); return 0;}/*12340012384 12039847102934------------------------49360049536 370 20037152 111060111456 24680024768 12340012384 86380086688 49360049536 987200 99072 111060111456 37020037152 24680024768 12340012384------------------------148571862351672082734656*/
View Code

"Algorithmic Learning Notes" 35. High-precision vertical multiplication SJTU OJ 1274

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.