Q - 高精度(大数)n次方

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
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
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++

while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}
没做大数乘法,直接上手这题,我经历了一个晚上,和一个下午.我看懂了别人的思想,但到自己用时却bug不断。首先是我直接定义string r作为大数的输入.然后在将它转化为数字数组时,编译器报错,至今不知道为什么,无耐改为用字符串r[6],再解决了这个之后,问题还是很多,主要是一些小问题。整体思想就是按找笔算的乘法方式实现,还要处理进位。 最后的输出也是个大问题,因为要把无意义的0给去掉。比如1.1000,输出时应为1.1.   还有0.0001输出应为。0001. 还有更变态的1.0000.输出应为1 多个点不合适吧。输出的思路是:以小数点位置为中心,从两边向其靠拢。遇到第一个大于0的数便记录下来。
   
   
#include<iostream> #include<cstring> #include<algorithm> using namespace std; short result[160];//存储结果 short temp[160];//临时存储结果 short num[6];//存储数字 void rpow(char r[],int n); int main() {  char r[6];//我用string报错,改用字符数组  int n;  while(cin>>r>>n)  {     if(n==0)//测试不要求n等于0,   {    cout<<1<<endl;   }   else   {    rpow(r,n);      }
 } } void  rpow(char r[],int n) {  int i,j,k,place=0;  for(i=0;i<160;i++)//初始化各个数组  {   result[i]=temp[i]=0;  }  for(i=0;i<6;i++)  {   num[i]=0;  }  for(i=5,j=0;i>=0;--i)  {   if(r[i]!='.')   {   num[j]=result[j]=temp[j]=r[i]-'0';   j+=1;   }   else   {    place=(5-i)*n;//记录小数点位数,并乘n得到结果的小数点位数   }  }  while(n>=2)//大于2时计算  {   n--;   for(i=0;i<160;i++)//每次循环清零。   {    result[i]=0;   }   for(i=0;i<5;i++)   {    int sum;    for(j=0;j<160;j++)    {     if(num[i]==0)     {      break;     }     sum=num[i]*temp[j];     result[i+j]+=sum;     for(k=i+j;result[k]>9;k++)//进位处理     {      result[k+1]+=result[k]/10;      result[k]=result[k]%10;     }    }   }   for(i=0;i<160;i++)//转存   {    temp[i]=result[i];   }  }  int flag1=-1,flag2=-1;  for(i=159;i>=place;i--)//记录结果第一个不为零的数  {   if(result[i]>0)   {    flag1=i;    break;   }  }  for(i=0;i<place;i++)//同上  {   if(result[i]>0)   {    flag2=i;    break;   }  }  if(flag1!=-1)  {   while(flag1>=place)   {    cout<<result[flag1];    flag1--;   }  }  if(flag2!=-1)  {   cout<<".";   place--;   while(place>=flag2)   {    cout<<result[place];    place--;   }    }  cout<<endl; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值