10106 - Product
高精度乘法题目,用了LRJ白书的模板。WA了许多次。
注意点:1.前导零需要过滤,应该写个函数(我直接偷懒用加法搞定了)
2.乘法、加法的进位。
最后,模板不一定是最好的,适合自己的,自己写的出来的才是最好的。
最后再送几组测试用例
input:
0
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
2
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9239538994338789151251557336598603050049616822294053424980870425131464466182353830814916071789845042370142421852990439213459450627013510493542232079586565952646636598169299756958773885685805728065441653009173744078838973219505500897158270221204367245
9896336400014021081112872660026786571823539120528457013481728314902092185310581449120208631014862091874106743950514845492688570616578189552019550880999626199354705816824577956144319588556564026758407650715151019940083959555421056787710369504463191742
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
Ouput:
0
19999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
91437586069023901335141066474711666537718790039237144985051289985350175432524770084824378427555002818097099309346160871667571286412924312245148206827831443006000924957609921550337260262953945785774998477067644833241060308469618958625980383949243282674332743009843156607612702745027529447743579967907340959122060977535403916274356199174936757805300947243257666366727345740624830784342794119256500258114097942703603683608916604350586797991674579535204948315352029368384842883219960490930872682219290790
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
input:
000000000 0000000000000000
0000005 000000000005
output:
0
25
Input:
4738787878
87897987
333
333
87897987
4738787878
00000000
0
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
Output:
416529915296201586
110889
416529915296201586
0
2469135802469135802469135802469135802469135802469135802469135802469135802469135802469135802469135802469
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn=10000; struct bign { int len,s[maxn],flag;//flag 为表示正负备用 bign() {memset(s,0,sizeof(s));len=1;} bign operator = (const char* num) { len=strlen(num); for(int i=0;i<len;i++) s[i]=num[len-i-1]-'0'; return *this; } bign operator = (int num) { char s[maxn]; sprintf(s,"%d",num); *this=s; return *this; } bign(int num){ *this=num; } bign(const char* num){ *this=num;} string str() const { string res=""; for(int i=0;i<len;i++) res=(char)(s[i]+'0')+res; if(res=="") res="0"; return res; } bign operator + (const bign& b) const { bign c; c.len=0; int max=0; if(len>b.len) max=len; else max=b.len; for(int i=0,g=0;g || i<max;i++) { int x=g; if(i<len) x+=s[i]; if(i<b.len) x+= b.s[i]; c.s[c.len++]=x%10; g=x/10; } int flag=1; for(int i=c.len;i>=0;i--) { //cout<<"DEBUG:"<<i<<" "<<c.s[i]<<endl; if(c.s[i]!=0) { flag=0; c.len=i+1; break; } } if(flag) c.len=0; //cout<<"DEBUG "<<str()<<" "<<b.str()<<" "<<c.str()<<endl; return c; } bign operator * (const bign& b) const { bign c,tmp=0; int x; for(int i=0;i<len;i++) for(int j=0;j<b.len;j++) { c.s[i+j]+=b.s[j]*s[i]; if(c.s[i+j]>=10) { c.s[i+j+1]+=c.s[i+j]/10; c.s[i+j]=c.s[i+j]%10; } } c.len=b.len+len+1; if(c.s[c.len]==0) c.len--; c=c+tmp; return c; } }; istream& operator >> (istream &in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, const bign& x) { out << x.str(); return out; } int main() { bign a,b; bign tmp=0; while(cin>>a>>b) { a=a+tmp; b=b+tmp; bign c=a*b; cout<<c<<endl; } }