Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
拜托各位高手帮忙看看吧,能测得数据都测过了,也都通过了呀,可提交总是WA呀下面是源码: #include <iostream.h> #include <string.h> #include <stdlib.h> #define LENGTH 1000 typedef struct NumberType { int digit[LENGTH]; int precision; int n;//refer the length of all }; typedef NumberType* BigNumber; void Multiply(BigNumber A,BigNumber B,BigNumber C);//C=A*B void transform(char* str,BigNumber number); void Print(BigNumber sum); void clearzero(BigNumber A); int main() { char str[10]; int power; BigNumber number,temp,sum; number=(BigNumber)malloc(sizeof(NumberType)); temp=(BigNumber)malloc(sizeof(NumberType)); sum=(BigNumber)malloc(sizeof(NumberType)); while(cin>>str>>power) { int i,j; transform(str,number); for (j=0;j<LENGTH;j++) temp->digit[j]=number->digit[j]; temp->n=number->n; temp->precision=number->precision; for(i=1;i<power;i++) { clearzero(number); clearzero(temp); Multiply(number,temp,sum); for (j=0;j<LENGTH;j++) temp->digit[j]=sum->digit[j]; temp->n=sum->n; temp->precision=sum->precision; } Print(sum); } return 0; } void Multiply(BigNumber A,BigNumber B,BigNumber C) { int i,j,n; int carry1=0,carry2=0;//carry1 for mul, carry2 for add NumberType temp; BigNumber ptemp=&temp; for (i=0;i<LENGTH;i++) C->digit[i]=0; C->n=0; C->precision=0; for (i=0;i<A->n;i++) { for (j=0;j<B->n;j++) { C->digit[i+j]+=A->digit[i]*B->digit[j]; C->digit[i+j+1]+=C->digit[i+j] / 10; C->digit[i+j] %= 10; } } C->precision=A->precision+B->precision; n=A->n+B->n; while ((C->digit[n-1]==0)&&(n>C->precision)) n--; C->n=n; } void transform(char* str,BigNumber number) { int i,j=-1,k=0; int length=strlen(str); for (i=length-1;i>=0;i--) { if (str[i]!='.') number->digit[length-1-i-k]=str[i]-'0'; else { j=i; k=1; } } if (j==-1) { number->n=length; number->precision=0; } else { number->n=length-1; number->precision=number->n-j; } } void Print(BigNumber sum) { int i; if (sum->n==0) { cout<<"0\n"; return; } for (i=sum->n;i>0;i--) { if (i==sum->precision) cout<<'.'; cout<<sum->digit[i-1]; } cout<<endl; } void clearzero(BigNumber A) { int i,pos; pos=0; while ((A->digit[pos]==0)&&(pos<A->precision)) pos++; if (pos!=0) { for (i=pos;i<A->n;i++) A->digit[i-pos]=A->digit[i]; A->n=A->n-pos; A->precision-=pos; } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator