您的位置:首页 > 其它

求高精度幂-----Exponentiation

2014-03-19 20:19 176 查看
Description

对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。

现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
Input

T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output

对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
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

代码如下:

#include<iostream>
using namespace std;int main()
{
 char ch[6];
 int power;
 while(cin>>ch>>power)
 {
  int result[125]={0};
  int tail=5,head=0;
  int after=0;
  while(ch[tail]=='0')
   tail--;
  while(ch[head]!='.')
   head++;
  after=tail-head;
  int i=0;
  int base=0;
  int p=1;
  int pow=power;
  while(tail>=0)
  {
   if(ch[tail]!='.')
   { 
    result[i]=ch[tail]-'0';
    base+=(ch[tail]-'0')*p;
    i++;
    p*=10;
   }
   tail--;
  }
  while(power>1)
  {
   tail=0;
   while(tail<i)
   {
    result[tail]=base*result[tail];
    tail++;
   }
   int j=0;
   int before=0;
   int mid=0;
   while(j<i)
   {
    mid=(result[j]+before)%10;
    before=(result[j]+before)/10;
    result[j]=mid;
    j++;
   }
   while(before>0)
   {
    result[i]=before%10;
    before/=10;
    i++;
   }
   power--;
   
  }
  after*=pow;
  if(after>=i)
    {
     int zero=after-i;
      cout<<".";
      while(zero>0)
      {
      cout<<"0";
        zero--;
      }
      i--;
      while(i>=0)
      {
       cout<<result[i];
        i--;
      }
                
  }  
    else if(after<i)
    {
     i--;
      while(i>=0)
      {
       if(after-i==1)
         cout<<"."<<result[i];
        else
          cout<<result[i];
        i--;
   }    
             
  }
  cout<<endl;
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: