POJ 1001 Exponentiation
来源:互联网 发布:php就业形势 编辑:程序博客网 时间:2024/05/18 21:11
【题意】给定多组数组,每组数据有实数R,n,求R^n
( 0.0 < R < 99.999,0 < n <= 25 )
【分析】
实数,直接求精度会爆掉,那就用高精度乘法。
至于小数点的问题,读入时有t位小数,乘法后就有t*n位小数。
首先用字符串读入,然后把t求出,把数字存到a数组中,用b数组进行运算。
具体过程是v[i+j-1]+=a[i]*b[j],就是普通的高精度。
然后关键是结果的处理,本题要求①省略可省略的0,若整数为0,小数非0则整数的0也省去 ②若小数后全为0则忽略小数点;
我两次WA在第②点,审题审错了......
结果的处理,用l,r表示输出的区间,然后遇到0就移位,忽略掉0的存在。
然后按照数输出就AC了。
【代码】
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int L=200;
char s[L];
int n,t,a[L],b[L],v[L];
void init(void)
{
memset(a,0,sizeof a);
memset(b,0,sizeof b);
for (int i=5;i+1;i--) if (s[i]=='.') t=n*(5-i); else a[++a[0]]=s[i]-'0';
b[0]=b[1]=1;
}
void work(void)
{
for (;n--;)
{
memset(v,0,sizeof v); v[0]=1;
for (int i=1;i<=a[0];i++)
for (int j=1;j<=b[0];j++) v[i+j-1]+=a[i]*b[j];
for (;v[0]<=a[0]+b[0]-1||v[v[0]+1];v[0]++)
v[v[0]+1]+=v[v[0]]/10,v[v[0]]%=10;
if (!v[v[0]]) v[0]--;
memmove(b,v,sizeof v);
}
int l=1,r=b[0];
for (;!b[l]&&l<=t;l++);
for (;!b[r]&&r>=t;r--);
for (int i=r;i>t;i--) printf("%d",b[i]);
if (l<=t)
{
printf(".");
for (int i=t;i>=l;i--) printf("%d",b[i]);
}
printf("\n");
}
int main(void)
{
for (;~scanf("%s%d",s,&n);)
{
init();
work();
}
return 0;
}
【小结】
for (S1;S2;S3)的执行顺序为S1,S3,S2,S3,S2,S3,S2......
所以对于for (;!b[l]&&l<=t;l++); ,l为1时判断了两次。
- 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 高精度
- 杭电-2051 Bitset
- NOIP 2014 提高组 Day2 T2 寻找道路
- 四,从头到尾在myeclipse中的java开发环境搭建之SQLServer2000数据库的安装
- 杭电-2099 整除的尾数
- NOIP 2014 提高组 Day2 T1 无线网路发射器选址
- POJ 1001 Exponentiation
- POJ 1002 487-3279
- C#设计模式(十三)迭代器模式
- POJ 1003 Hangover
- 用 Tomcat 和 Eclipse 开发 Web 应用程序
- POJ 1004 Financial Management
- POJ 1005 I Think I Need a Houseboat
- Tomcat全攻略
- POJ 1006 Biorhythms