大数计算_乘法
发布时间:2020-12-14 04:11:25 所属栏目:大数据 来源:网络整理
导读:/*limit:a、b0*/#includeiostreamusing namespace std;#includestringstring add(string x,string y){string sum;int index_x=x.size()-1,index_y=y.size()-1;int num=0,tmp=0; while(index_x=0index_y=0){ int num_x=x[index_x]-48,num_y=y[index_y]-48; nu
/*limit:a、b>0*/ #include<iostream> using namespace std; #include<string> string add(string x,string y) { string sum; int index_x=x.size()-1,index_y=y.size()-1; int num=0,tmp=0; while(index_x>=0&&index_y>=0){ int num_x=x[index_x]-48,num_y=y[index_y]-48; num=num_x+num_y+tmp; string temp; if(num>=10)tmp=num/10; else tmp=0; temp+=(num%10+48); sum.insert(0,temp); index_x--;index_y--; } if(tmp&&index_x<0&&index_y<0){string temp;temp+=(tmp+48);sum.insert(0,temp);} else{ while(index_x>=0){ string temp; int num=x[index_x]-48+tmp; if(num>=10)tmp=num/10; else tmp=0; temp+=(num%10+48); sum.insert(0,temp); index_x--; } while(index_y>=0){ string temp; int num=y[index_y]-48+tmp; if(num>=10)tmp=num/10; else tmp=0; temp+=(num%10+48); sum.insert(0,temp); index_y--; } if(tmp){string temp;temp+=(tmp+48);sum.insert(0,temp);} } return sum; } string mul(string a,string b) { int len_b=b.size(); string sum; int mark=1; while(len_b>0){ len_b--; int len_a=a.size(); int k=1*mark; mark*=10; string pre; while(len_a>0){ int temp=(a[--len_a]-48)*(b[len_b]-48); string post; if(temp/10!=0){post+=temp/10+48;post+=temp%10+48;} else post+=temp+48; int counter=k; k*=10; while(counter>1){ post+='0'; counter/=10; } pre=add(pre,post); } sum=add(sum,pre); } return sum; } void main() { string a,b; while(cin>>a>>b) cout<<mul(a,b)<<endl; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |