July 2015 school recruitment-Huawei computer pen questions-multiplication of large numbers

Source: Internet
Author: User
Tags mul
#include "stdafx.h"#include<string>#include<iostream>using namespace std;void add(string &sum,string temp){int len1=sum.size();int len2=temp.size();int jw=0;int i,j;for(i=len1-1,j=len2-1;i>=0 && j>=0;i--,j--){int a=sum[i]-'0';int b=temp[j]-'0';int c=a+b+jw;if(c>9){sum[i]=c-10+'0';jw=1;}else{sum[i]=c+'0';jw=0;}}if(jw==1){while(jw==1){int a=sum[j]-'0';a++;if(a>9){sum[j]='0'+a-10;jw=1;}else{sum[j]='0'+a;jw=0;}}}}string mul(string sum,char ch,int off){int len=sum.size();int a=ch-'0';int jw=0;for(int i=len-1;i>=0;i--){int b=sum[i]-'0';int c=a*b+jw;jw=c/10;int d=c%10;sum[i]=d+'0';}if(jw!=0){sum=string(1,'0'+jw)+sum;}sum=sum+string(off,'0');return sum;}string get_result(string str1,string str2){string sum(200,'0');int len=str2.size();for(int i=len-1;i>=0;i--){string temp=mul(str1,str2[i],len-1-i);add(sum,temp);}int index=sum.find_first_not_of('0');sum=sum.substr(index);return sum;}int main(){string str1="56789";string str2="555432";string result=get_result(str1,str2);cout<<result<<endl;system("pause");return 0;    }

July 2015 school recruitment-Huawei computer pen questions-multiplication of large numbers

Related Article

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.