标签:溢出 sizeof cstring UI main logs memset problem 数据
2
1 2
123456789 987654321
Case 1: 1 * 2 = 2 Case 2: 123456789 * 987654321 = 121932631112635269
数组模拟,要细心。
#include<iostream> #include<cstring> #include<queue> using namespace std; int a[1000]={0},b[1000]={0},mm[5000]={0}; void daozhi(string s,int a[])//数据倒置存入数组中; { int i,m=0; for(i=s.size()-1;i>=0;i--) { a[m++]=s[i]-‘0‘; } } void rebuild(string s,string ss,int a[],int b[],int mm[])//乘法的模拟 { int n,k,i,j; for(i=0;i<s.size();i++) { n=i; for(j=0;j<ss.size();j++) { k=(mm[n]+(a[i]*b[j])%10)/10; mm[n]=(mm[n]+(a[i]*b[j])%10)%10; mm[n+1]+=((a[i]*b[j])/10+k); n++; } } } int main() { int n,count,i; cin>>n; count=n; while(n--) { string c,d; cin>>c>>d; cout<<"Case "<<count-n<<":"<<endl; daozhi(c,a); daozhi(d,b); rebuild(c,d,a,b,mm); cout<<c<<" *"<<" "<<d<<" ="<<" "; for(i=4999;i>=0;i--) { if(mm[i]) break; } for( ;i>=0;i--) { cout<<mm[i]; } cout<<endl; if(n) cout<<endl; memset(mm,0,sizeof(mm)); } return 0; }
标签:溢出 sizeof cstring UI main logs memset problem 数据
原文地址:http://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/6607691.html