Exponentiation幂指数
来源:互联网 发布:普通话考试模拟软件 编辑:程序博客网 时间:2024/04/30 15:10
引用:
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 67973 Accepted: 15908
Description
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
Output
Sample Input
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
Hint
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 */
{
...
}
分析:
题目要求输入实数R,范围0.0~99.999,有效数字5位。幂指数n,范围0~25,整数.
难点在于乘法计算过程中,float型实数最大只有3.4E +/- 38 (7 digits)。不可能作为结果,数据会溢出。
对策:把乘数、被乘数拆为只有1位的整数,存入分配的整形数组。
我的代码(作参考,可实现目标。未测试"计算优化")
#include <conio.h>
#include <iostream>
using namespace std;
struct R_Input
{
float R;
int n;
struct R_Input* next;
};
//申明函数
int Add(int* R, int num); //*R,num 0-9
void Cal(R_Input* rTail);
//input data //计算 cout<<"Sample Output"<<endl; rTail=rTail->next; const int SPACE=256; void Cal(R_Input* rTail) //输入实数R if(1) //幂运算 int multiplier[6]; int Exp=rTail->n; while(Exp>1) Exp--; char ch[SPACE]={0}; int where_0; for(;prt_n>=0;prt_n--) }
int main()
{
cout<<"R:0.0000<R<99.999 and n:0<n<=25"
<<"/nInput any character excluding number to quit."<<endl;
float R_In=0;
int n=0;
R_Input R_cin;
R_Input* rTail;
cin>>R_In>>n;
R_cin.R=R_In;
R_cin.n=n;
R_cin.next=NULL;
rTail=&R_cin;
while(cin>>R_In>>n)
{
R_Input* s=new R_Input;
s->R=R_In;
s->n=n;
s->next=NULL;
rTail->next=s;
rTail=s;
}
rTail=&R_cin;
while(rTail)
{
Cal(rTail);
}
_getch();
return 0;
}
int Add(int* R, int num)
{
int m,res;
m=*R;
res=m+num;
if(res<10) *R=res;
else
{
*R=res%10;
Add(R+1,res/10);
return 1;
}
return 0;
}
{
int ans[SPACE]={0};
int lenInt,lenDec;
{
int k=(int)rTail->R;
int l=(int)((rTail->R-k)*10000);
for(int i=1;i<5;ans[i-1]=l%10,l=l/10,i++);
l=(int)((rTail->R-k)*100000);
if((l-l/10*10)!=0) Add(ans,1);
for(int i=4;i<=5;ans[i]=k%10,k=k/10,i++);
}
multiplier[0]=ans[0];
multiplier[1]=ans[1];
multiplier[2]=ans[2];
multiplier[3]=ans[3];
multiplier[4]=ans[4];
multiplier[5]=ans[5];
{
int An[256];
for(int i=0;i<256;i++)
{
An[i]=ans[i];
ans[i]=0;
}
int len;
for(len=SPACE-1;!An[len];len--);
for(int i=0;i<=len;i++)
for(int j=0;j<6;j++)
{
int A,B,Mi,Mj,Ex10;
for(Mi=1;Mi<=i;Mi++);
for(Mj=1;Mj<=j;Mj++);
//计算A*B
Ex10=Mi+Mj-2;
A=An[i];
B=multiplier[j];
if(A*B!=0)
Add(&ans[Ex10],A*B);
}
}
for(where_0=SPACE-1;ans[where_0]==0 && where_0>0;where_0--);
//获取小数、整数部分数位
lenDec=4*rTail->n;
lenInt=where_0-lenDec+1;
int prt_n=where_0;
int chEnd=0;
for(;lenInt>0;prt_n--,lenInt--)
{
// printf("%d",ans[prt_n]);
sprintf(&ch[chEnd++],"%d",ans[prt_n]);
}
// printf(".");
if(lenInt<0)
{
sprintf(&ch[chEnd++],"0");
prt_n=prt_n-lenInt;
}
sprintf(&ch[chEnd++],".");
{
// printf("%d",ans[prt_n]);
sprintf(&ch[chEnd++],"%d",ans[prt_n]);
}
// printf("/n");
for(chEnd--;chEnd>=0 && ch[chEnd]=='0';chEnd--);
ch[chEnd+1]='/0';
cout<<ch<<endl;
- Exponentiation幂指数
- POJ 1001 Exponentiation 高精度指数
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
- poj1001 Exponentiation 小数幂
- poj1001-Exponentiation(高精度幂)
- POJ1001 Exponentiation(高精度幂)
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- ChangeServiceConfig2设置SERVICE_CONFIG_FAILURE_ACTIONS
- jQuery调用WebService(.net)
- 获取扩展的版本号信息
- 从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇
- 从多角度看软件系统需求(节选) 文/卢琳生
- Exponentiation幂指数
- CSstring 按格符分成数组
- 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件
- A good Linux document on line
- svn服务器架设
- 进程间通信-匿名管道
- 从零开始学习 ASP.NET MVC 1.0 (三) Controller/Action 深入解析与应用实例
- 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解
- ASP.NET配置文件Web.config 详细解释