POJ1001·Exponentiation

来源:互联网 发布:屏幕录制软件免费版 编辑:程序博客网 时间:2024/06/09 20:31

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 */
{
...
}
#include <stdio.h>#include <iostream>#include <string>using namespace std;int main(){string mlp;//乘数int power;//乘数的幂int r[151];//保存结果int hdot;//保存小数点位置 while(cin>>mlp>>power){hdot=0;for(int t=0;t<150;t++)//初始化结果数组 {r[t]=-1;}if(mlp.find(".")!=string::npos)hdot=mlp.length()-mlp.find(".")-1;//记录小数点位置 string::iterator itr=mlp.end()-1;//迭代器指向最后一个字符 while(hdot>0&&itr>=mlp.begin())//去掉末尾无效的0 {if(*itr!='0'){break;}hdot--;itr--;}int cn=0;while(itr>=mlp.begin())//乘数第一次向结果数组中赋值 {if(*itr!='.'){r[cn]=*itr-'0';cn++;//结果数组长度或者说位数 }itr--;}int k=cn-1;int m=0;//保存临时数while(k>-1){m=m*10+r[k];//乘数的逆序??? k--;}for(int i=1;i<power;i++)//计算指数次方,方法很巧妙,自叹不如 {int j=0;while(r[j]>-1){r[j]=r[j]*m;j++;}j=0;while(r[j]>-1){if(r[j+1]==-1&&r[j]>=10)r[j+1]=r[j]/10;elser[j+1]+=r[j]/10;r[j]=r[j];j++;}} hdot=hdot*power;//寻找小数点位置 int cnt=0;while(r[cnt]>-1){cnt++;}if(hdot>=cnt){cout<<".";while(hdot>cnt){cout<<"0";hdot--;}hdot=0;}for(k=cnt-1;k>=0;k--){if((k+1)==hdot&&hdot!=0)cout<<".";cout<<r[k];}cout<<endl;}return 0;}



原创粉丝点击