Exponentiation幂指数

来源:互联网 发布:普通话考试模拟软件 编辑:程序博客网 时间:2024/04/30 15:10

引用:


Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 67973 Accepted: 15908

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 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

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


分析:

题目要求输入实数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);


int main()
{


 cout<<"R:0.0000<R<99.999 and n:0<n<=25"
  <<"/nInput any character excluding number to quit."<<endl;

 //input data
 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;

 cout<<"Sample Output"<<endl;
 while(rTail)
 {
  Cal(rTail);

  rTail=rTail->next;
 }

 
 _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;
}

const int SPACE=256;

void Cal(R_Input* rTail)
{
 int ans[SPACE]={0};
 int lenInt,lenDec;

 //输入实数R

 if(1)
 {
  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++);
 }

 //幂运算

 int multiplier[6];
 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 Exp=rTail->n;

 while(Exp>1)
 {
  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);
    
   }

  Exp--;
 }

 

 char ch[SPACE]={0};

 int where_0;
 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++],".");

 for(;prt_n>=0;prt_n--)
 {
//  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;

}

 

原创粉丝点击