高精度数幂运算

小榕流光 2011-10-23 06:37:54
Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 93983 Accepted: 22472

Description

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 Rn 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 */
{
...
}
Source






我的源码如下,测试数据没有错误,但是提交网站的时候总报错,不知道为什么,各位大侠救救我!

#include <iostream>
#include<cstring>
#include <cmath>
#define maxsize 200
using namespace std;
int main()
{
int c[maxsize]={0},a[maxsize]={0};
int g1=0,g2=0,n,i,j,k2=0,k1,g=0,k3=0,r=0,g3=0;
char R[maxsize];

while(cin>>R>>n)
{
k1=strlen(R);
for(i=0;i<k1;i++)
{
if(R[k1-i-1]=='.'){
g++;//;有小数点的标记
k2=i;//小数点位置
}
else a[i-g]=R[k1-i-1]-'0';//将字符串转化为整形数组
}
c[0]=1;
i=0;
while(i<k1-g)
{ r=r+a[i]*pow(10.0,i);i++; }//转化成一个整数
if(r==0)
{
cout<<"0"<<endl;
continue;
}
k1=k1-g;
//k3=k1-1;
k3=0;
for(i=0;i<n;i++)//次数
{ for(j=0;j<=k3;j++)//每位数乘
c[j]=c[j]*r;
j=0;
while(c[j]>=10||j<k3)
{
c[j+1]=c[j+1]+c[j]/10;
c[j]=c[j]%10;
j++;
}
k3=j;
}

j=0; k2=k2*n;
if(k2>k3) k3=k2-1;//考虑是纯小数的情形前要加零
while(c[g3]==0) g3++;//考虑后面都是零的情形要去掉

for(i=k3;i>=g3;i--)
{
if(g&&(i+1==k2))//小数点位置的判断
{
cout<<".";
cout<<c[i];
}
else cout<<c[i];
}
for(i=k3;i>=0;i--)c[i]=0;//初始化c数组和各种参数

g1=0,g2=0,k2=0,g=0,k3=0,r=0,g3=0;
cout<<endl;
}

return 0;
}
...全文
359 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
turing-complete 2011-10-23
  • 打赏
  • 举报
回复
#include <iostream>
#include<cstring>
#include <cmath>
#define maxsize 200
using namespace std;
int main()
{
int c[maxsize]={0},a[maxsize]={0};
int g1=0,g2=0,n,i,j,k2=0,k1,g=0,k3=0,r=0,g3=0;
char R[maxsize];

while(cin>>R>>n)
{
k1=strlen(R);
for(i=0;i<k1;i++)
{
if(R[k1-i-1]=='.'){
g++;//;有小数点的标记
k2=i;//小数点位置
}
else a[i-g]=R[k1-i-1]-'0';//将字符串转化为整形数组
}
c[0]=1;
i=0;
while(i<k1-g)
{ r=r+a[i]*(int)pow(10.0,i);i++; }//转化成一个整数
if(r==0)
{
cout<<"0"<<endl;
continue;
}
k1=k1-g;
//k3=k1-1;
k3=0;
for(i=0;i<n;i++)//次数
{ for(j=0;j<=k3;j++)//每位数乘
c[j]=c[j]*r;
j=0;
while(c[j]>=10||j<k3)
{
c[j+1]=c[j+1]+c[j]/10;
c[j]=c[j]%10;
j++;
}
k3=j;
}

j=0; k2=k2*n;
if(k2>k3) k3=k2-1;//考虑是纯小数的情形前要加零
while(c[g3]==0) g3++;//考虑后面都是零的情形要去掉

for(i=k3;i>=g3;i--)
{
if(g&&(i+1==k2))//小数点位置的判断
{
cout<<".";
cout<<c[i];
}
else cout<<c[i];
}
for(i=k3;i>=0;i--)c[i]=0;//初始化c数组和各种参数

g1=0,g2=0,k2=0,g=0,k3=0,r=0,g3=0;
cout<<endl;
}

return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧