【POJ】1001 Exponentiation
来源:互联网 发布:access数据库的用法 编辑:程序博客网 时间:2024/06/11 03:35
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.
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
大数,求一个数R的n次幂。
具体思路是把小数点移除,记录小数点的位置,转化成整数大数进行循环乘积,
再把小数点插入回已经乘积完成的大数数组中。
记得注意小数点末尾0的消除以及大数的进位。
怒贴代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;const int size=1000; //大数位数void mult(char* A,char* B,char* ans){int i,j,k;int fract; //总小数个数int dot=-1; //小数点位置for(k=0;A[k]!='\0';k++)if(A[k]=='.')dot=k;int lena=k;if(dot==-1)fract=0;elsefract=lena-dot-1;dot=-1;for(k=0;B[k]!='\0';k++)if(B[k]=='.')dot=k;int lenb=k;if(dot==-1)fract+=0;elsefract+=(lenb-dot-1); //总小数个数int a[size+1]={0};int b[size+1]={0};int pa=0,pb=0;/*倒序*/for(i=lena-1;i>=0;i--){if(A[i]=='.')continue;a[pa++]=A[i]-'0';}for(j=lenb-1;j>=0;j--){if(B[j]=='.') //暂时删除小数点continue;b[pb++]=B[j]-'0';}int c[2*size+1]={0};int lenc;for(pb=0;pb<lenb;pb++){int w=0; //低位到高位的进位for(pa=0;pa<=lena;pa++) // = 为了处理最后的进位{int temp=a[pa]*b[pb]+w;w=temp/10;temp=(c[pa+pb]+=temp%10);c[lenc=pa+pb]=temp%10;w+=temp/10;}}/*倒序,得到没有小数点的ans*/for(pa=0,pb=lenc;pb>=0;pb--)ans[pa++]=c[pb]+'0';ans[pa]='\0';lena=pa; /*插入小数点*/bool flag=true; //标记是否需要删除小数末尾的0if(fract==0) //小数位数为0,无需插入小数点flag=false;else if(fract<lena) //小数位数小于ans长度,在ans内部插入小数点{ans[lena+1]='\0';for(i=0,pa=lena;pa>0;pa--,i++){if(i==fract){ans[pa]='.';break;}elseans[pa]=ans[pa-1];}}else //小数位数大于等于ans长度,在ans前面恰当位置插入小数点{char temp[size+1];strcpy(temp,ans);ans[0]='0';ans[1]='.';for(int i=0;i<fract-lena;i++) //补充0ans[i+2]='0';for(j=i,pa=0;pa<lena;pa++)ans[j++]=temp[pa];ans[j]='\0';}/*删除ans小数末尾的0*/if(flag){lena=strlen(ans);pa=lena-1;while(ans[pa]=='0')ans[pa--]='\0';if(ans[pa]=='.') //小数全为0ans[pa--]='\0';}/*删除ans整数开头的0,但至少保留1个0*/pa=0;while(ans[pa]=='0') //寻找ans开头第一个不为0的位置pa++;if(ans[pa]=='\0') //没有小数{ans[0]='0';ans[1]='\0';}else //有小数{for(i=0;ans[pa]!='\0';i++,pa++)ans[i]=ans[pa];ans[i]='\0';}return;}char a[size+1];char ans[size*size+1];int main(void){int b;while(cin>>a>>b){memset(ans,'\0',sizeof(ans));ans[0]='1';ans[3]='\0';for(int i=1;i<=b;i++)mult(a,ans,ans);cout<<ans<<endl;}return 0;}
0 0
- POJ 1001 Exponentiation
- POJ 1001 Exponentiation
- poj 1001 Exponentiation
- POJ 1001 "Exponentiation"
- poj 1001 Exponentiation
- POJ 1001 Exponentiation
- poj 1001 exponentiation
- 【POJ】1001 Exponentiation
- POJ 1001 Exponentiation
- poj 1001Exponentiation
- 《POJ 1001》 Exponentiation
- POJ 1001 Exponentiation
- [POJ]1001 Exponentiation
- POJ-1001-Exponentiation
- [poj] 1001 Exponentiation
- POJ 1001 Exponentiation
- poj 1001 Exponentiation
- POJ-1001 Exponentiation 高精度
- 报错:spring整合Hibernate
- 错误"The encoder 'aac' is experimental but experimental codecs are not enabled"
- android工程下运行main方法的配置方法
- Eclipse自动生成注释模版
- 根据所选择的 TrueType 字体生成点阵数据
- 【POJ】1001 Exponentiation
- android SQLiteOpenHelper使用示例
- Unity3D常见问题及使用技巧汇总(更新中...)
- 原子操作 vs 非原子操作
- AFNetworking 2.0
- MAC 下virtualbox虚拟机串口设置方法
- KLOXO启用CURL
- js关闭浏览器
- Android应用:霓虹灯