http://poj.org/problem?id=1001
WA了三次
整数的幂忘记去掉小数点了!!!!!
Time Limit: 500MS | Memory Limit: 10000K |
Description
Input
Output
Sample Input
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
/* Author : yan * Question : POJ 1001 Exponentiation * Date && Time : Monday, January 31 2011 11:20 PM * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 */ #include<stdio.h> #define MAX 500 #define bool _Bool #define true 1 #define false 0 char ans[2*MAX]; char stack[2*MAX]; int top; void multiply(char* a,char* b,char* c) { int i,j,ca,cb,* s; ca=strlen(a); cb=strlen(b); s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++) s[i]=0; for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i]-'0')*(b[j]-'0');//i+j+1的目的就是为了防止最高位进位而产生错误 for (i=ca+cb-1;i>=0;i--) if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } i=0; while (s[i]==0) i++;//去除前导0 for (j=0;i<ca+cb;i++,j++) c[j]=s[i]+'0'; c[j]='/0';//将结果存储到字符数组 free(s); } int del_point(char *ch) { int len,i,res; len=strlen(ch); for(i=0;i<len;i++) if(ch[i]=='.') { res=len-i-1; break; } for(;i<len-1;i++) ch[i]=ch[i+1]; ch[len-1]='/0'; return res; } void add_point(char *ch,int pos) { int i,len; bool before=false; len=strlen(ch); if(len>pos) before=true; top=0; pos++; while(pos--) if(len<0) stack[top++]='0'; else stack[top++]=ch[len--]; stack[top++]='.'; while(len>0) { stack[top++]=ch[len--]; } if(before) stack[top++]=ch[0]; } void print_stack() { int i=0; while(top--) ans[i++]=stack[top]; ans[i]='/0'; } void del_zero() { int len; len=strlen(ans); while(len--) { if(ans[len]=='0') ans[len]='/0'; else break; } len=strlen(ans); if(ans[len-1]=='.') ans[len-1]='/0'; } int main() { //freopen("input","r",stdin); //freopen("output","w",stdout); char base[7]; int index; int pos,i; while(scanf("%s %d",base,&index)!=EOF) { pos=del_point(base); strcpy(ans,"1"); for(i=0;i<index;i++) { multiply(ans,base,ans); } add_point(ans,pos*index); print_stack(); //printf("%s/n",res); //printf("%s %d/n",ans,pos*index); del_zero(); printf("%s/n",ans); } return 0; }