时间:2016-05-09 22:01:19 星期一
题目编号:[2016-05-09][51nod][1027 大数乘法]
题目大意:给出2个大整数A,B,计算A*B的结果。
分析:大数相乘,直接上模板
#include<iostream>#include<iomanip>#include<string.h>using namespace std;const int dlen = 4;const int maxn = 9999;const int maxsize = 1000 + 10;class bignum{public: int a[500]; int len; bignum(){ len = 1; memset(a,0,sizeof(a)); } bignum operator * (const bignum & t)const{ bignum ret; int i,j,up; int tmp ,tmp1; for( i = 0 ; i < len ; ++i){ up = 0; for( j = 0 ; j < t.len ; ++j){ tmp = a[i] * t.a[j] + ret.a[i + j] + up; if(tmp > maxn){ tmp1 = tmp - tmp / (maxn + 1) * (maxn + 1); up = tmp / (maxn + 1); ret.a[i + j] = tmp1; }else { up = 0; ret.a[i + j] = tmp; } } if(up){ ret.a[i + j] = up; } } ret.len = i + j; while(ret.a[ret.len - 1] == 0 && ret.len > 1) --ret.len; return ret; } friend istream& operator >> (istream & in,bignum &b){ char ch[maxsize * 4]; int i = -1; in >> ch; int l = strlen(ch); int count = 0,sum = 0; int ed = 0; for(int i = l - 1 ; i >= ed ;){ sum = 0; int t = 1; for(int j = 0 ; j < 4 && i >= ed ; ++j,--i,t*=10){ sum += (ch[i] - '0') * t; } b.a[count++] =sum; } b.len = count++; return in; } friend ostream & operator << (ostream & out, const bignum &b){ int i; out<<b.a[b.len - 1]; for(i = b.len - 2 ; i >=0 ; --i){ out<<setw(4)<<setfill('0')<<b.a[i]; } return out; }};int main(){ bignum a,b; cin>>a>>b; cout<<a*b<<"\n"; return 0;}