我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

P2389——高精度乘法

Posted on 2008-09-03 19:14 Hero 阅读(167) 评论(0)  编辑 收藏 引用 所属分类: 代码如诗--ACM
  1 //2389 Accepted 452K 0MS G++ 2363B PKU
  2 
  3 //高精度乘法
  4 #include <iostream>
  5 using namespace std;
  6 
  7 const int Base=1000000000;
  8 const int Dlen=300;
  9 typedef long long llong;
 10 struct BigInt{
 11     int Len;
 12     int Data[Dlen];
 13     BigInt():Len(0){}
 14     BigInt(const BigInt &num):Len(num.Len){memcpy(Data,num.Data,Len*sizeof*Data);}
 15     BigInt(int num):Len(0){for(;num>0;num/=Base) Data[Len++]=num%Base;}
 16     BigInt &operator=(const BigInt &num){Len=num.Len;memcpy(Data,num.Data,Len*sizeof*Data);return *this;}
 17     int &operator[](int Index){return Data[Index];}
 18     int operator[](int Index)const{return Data[Index];}
 19 };
 20 int compare(const BigInt &A,const BigInt &B){
 21     if(A.Len!=B.Len) return A.Len>B.Len ? 1:-1;
 22     int i;
 23     for(i=A.Len-1;i>=0 && A[i]==B[i];i--);
 24     if(i<0)return 0;
 25     return A[i]>B[i] ? 1:-1;
 26 }
 27 BigInt operator+(const BigInt &A,const BigInt &B){
 28     int i,Carry(0);
 29     BigInt R;
 30     for(i=0;i<A.Len||i<B.Len||Carry>0;i++){
 31         if(i<A.Len) Carry+=A[i];
 32         if(i<B.Len) Carry+=B[i];;
 33         R[i]=Carry%Base;
 34         Carry/=Base;
 35     }
 36     R.Len=i;
 37     return R;
 38 }
 39 BigInt operator-(const BigInt &A,const BigInt &B){
 40     int i,Carry(0);
 41     BigInt R;
 42     R.Len=A.Len;
 43     for(i=0;i<R.Len;i++){
 44         R[i]=A[i]-Carry;
 45         if(i<B.Len) R[i]-=B[i];
 46         if(R[i]<0) Carry=1,R[i]+=Base;
 47         else Carry=0;
 48     }
 49     while(R.Len>0&&R[R.Len-1]==0) R.Len--;
 50     return R;
 51 }
 52 BigInt operator*(const BigInt &A,const int &B){
 53     int i;
 54     llong Carry(0);
 55     BigInt R;
 56     for(i=0;i<A.Len||Carry>0;i++){
 57         if(i<A.Len) Carry+=llong(A[i])*B;
 58         R[i]=Carry%Base;
 59         Carry/=Base;
 60     }
 61     R.Len=i;
 62     return R;
 63 }
 64 istream &operator>>(istream &In,BigInt &num){
 65     char Ch;
 66     for(num=0;In>>Ch;){
 67         num=num*10+(Ch-'0');
 68         if(In.peek()<=' 'break;
 69     }
 70     return In;
 71 }
 72 ostream &operator<<(ostream &Out,const BigInt &num){
 73     int i;
 74     Out<<(num.Len==0 ? 0:num[num.Len-1]);
 75     for(i=num.Len-2;i>=0;i--for(int j=Base/10;j>0;j/=10) Out<<num[i]/j%10;
 76     return Out;
 77 }
 78 
 79 BigInt Bint0(0) ;
 80 BigInt Bint1(1) ;
 81 
 82 BigInt ina, inb, inc ;
 83 BigInt out ;
 84 
 85 int main()
 86 {
 87     while( cin >> ina >> inb )
 88     {
 89         inc = Bint1 ; out = Bint0 ;
 90         forint i=0; i<inb.Len; i++ )
 91         {
 92             inc = ina * inb.Data[i] ;
 93             forint j=0; j<i; j++ ) inc = inc * Base ;
 94             out = out + inc ;
 95         }
 96         
 97 
 98         cout << out << endl ;
 99     }
100 
101     return 0 ;
102 }

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理