标签:strong bre continue for main show poi eal scanf
#include<iostream> #include<string> using namespace std; struct BigReal //高精度实数 { int len; //长度 int num[10000]; int point; //小数点位置 BigReal() { len=1; point=0; memset(num,0,sizeof(num)); } }; bool Read(BigReal& a) //读入一个大实数 { string s; int t,i; if(cin>>s) { a.len=s.size(); a.point=0; t=0; for(i=s.size()-1;i>=0;i--) { if(s[i]==‘.‘) { a.len--; a.point=t; continue; } a.num[t++]=s[i]-‘0‘; } return true; } else return false; } void Show(BigReal& a) { int i,pos; for(i=0;i<a.point && a.num[i]==0;i++) ; pos=i; if(a.point==a.len) { if(pos==a.point) { cout<<0<<endl; //0.0000000的情况 return ; } else cout<<‘.‘; //0.121313114的情况 } for(i=a.len-1;i>=0;i--) { cout<<a.num[i]; if(i==pos) break; //小数时后导零不输出 if(i==a.point) cout<<‘.‘; } cout<<endl; } BigReal Mul(const BigReal& a,const BigReal& b) { int i,j,len=0; BigReal c; for(i=0;i<a.len;i++) for(j=0;j<b.len;j++) { c.num[i+j]+=a.num[i]*b.num[j]; if(c.num[i+j]>=10) { c.num[i+j+1]+=c.num[i+j]/10; c.num[i+j]%=10; } } c.point=a.point+b.point; len=a.len+b.len; while(c.num[len-1]==0 && len>1&&len>c.point) len--; //处理长度,去掉前导零 if(c.num[len]) len++; c.len=len; return c; } int main() { BigReal a; int b; while(Read(a)&& scanf("%d",&b)==1) { BigReal ans; ans.num[0]=1; while(b--) ans=Mul(ans,a); Show(ans); } return 0; }
标签:strong bre continue for main show poi eal scanf
原文地址:http://www.cnblogs.com/wangkun1993/p/6329527.html