【大数模板+十进制+for_loop】Multiply Strings
发布时间:2020-12-14 03:07:12 所属栏目:大数据 来源:网络整理
导读:Multiply Strings ? Total Accepted:? 10575 ? Total Submissions:? 51788 My Submissions Given two numbers represented as strings,return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative
Multiply Strings? Total Accepted:?10575? Total Submissions:?51788 My SubmissionsGiven two numbers represented as strings,return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. class Solution { public: #define MAXN 200 //最大位数 @@@@error: MAXN as 100 is too little //to=axb int multi(int *a,int n,int *b,int m,int *to){ //to should have been set to zero to=axb for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ to[i+j]+=a[j]*b[i]; } } int t=sim(to,m+n-1); return t; } //十进制化s[] int sim(int *s,int n){//位数 int in=0; for(int i=0;i<n;i++){ int c=in+s[i]; s[i]=c%10,in=c/10; } int i; for(i=n;i<MAXN;i++){////@error for(i=n;i<MAXN;i++) i not declared,should be for(int i) if(in<=0) break; else s[i]=in%10,in/=10; } //@@@error:需要去掉头部多余的0 while(i>0&&s[i]==0) i--; return i+1; } //main函数 string multiply(string num1,string num2) { int a[MAXN],b[MAXN],c[MAXN]; memset(a,sizeof(a));memset(b,sizeof(a));memset(c,sizeof(a)); int n=num1.length(),m=num2.length(); for(int i=0;i<num1.size();i++) a[i]=num1[n-1-i]-'0'; for(int i=0;i<num2.size();i++) b[i]=num2[m-1-i]-'0'; int s=multi(a,n,b,m,c); string res=""; for(int i=s-1;i>=0;i--){ res+='0'+c[i]; } return res; } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |