poj1001 高精度
来源:互联网 发布:网络电视怎么连接网络 编辑:程序博客网 时间:2024/05/03 18:59
如题:http://poj.org/problem?id=1001
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){...}cwhile(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 */{...}
Source
思路:很繁琐的一道题,题目思路很清晰,高精度乘法,找准小数点的位数,输出时加进去小数点。实现起来对小数点位数的处理和各种边界(小数点影响的前导后导0的处理),一个细节处理不好,就崩了。
在每一次的乘法中没有处理前导0,而是在所有运算后同时处理上下界和小数点的位置。确定输出的上界i,下界j。然后输出即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[10];
int a[10];
int res[500];
int Multiple(int ma,int mr)
{
int i,j;
int k=ma+mr;
int c[500]={0};
for(i=1;i<=ma;i++)
for(j=1;j<=mr;j++)
{
c[i+j-1]+=a[i]*res[j];
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
}
for(i=1;i<=k;i++)
res[i]=c[i];
mr=k;
return mr;
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
while(~scanf("%s",str+1))
{
int n;
cin>>n;
memset(a,0,sizeof(a));
memset(res,0,sizeof(res));
int dot_pos=-1;
int i,j;
int len=strlen(str+1);
int ma=0,mr=0;
for(i=len;i>=1;i--)
if(str[i]=='.')
dot_pos=i;
else
a[++ma]=str[i]-'0';
mr=ma;
for(i=1;i<=ma;i++)
res[i]=a[i];
for(i=0;i<n-1;i++)
mr=Multiple(ma,mr);
if(dot_pos==-1)
{
for(i=mr;i>=1;i--)
printf("%d",res[i]);
printf("\n");
}
else
{
dot_pos=len-dot_pos;
dot_pos*=n;
int up=mr,down=1;
for(i=1;i<=mr;i++)
if(res[i]!=0)
{
down=i;
break;
}
for(i=mr;i>=1;i--)
if(res[i]!=0)
{
up=i;
break;
}
i=up;
j=down;
if(dot_pos>up)
i=dot_pos;
if(dot_pos<down)
j=dot_pos+1;
while(i>=j)
{
if(i==dot_pos)
printf(".");
printf("%d",res[i]);
i--;
}
printf("\n");
}
}
return 0;
}
- POJ1001 高精度
- poj1001 高精度
- POJ1001 Exponentiation 高精度乘法
- poj1001(高精度【乘法】)
- POJ1001 高精度乘法
- POJ1001 Exponentiation 高精度
- poj1001(高精度)
- POJ1001 Exponentiation,高精度乘法
- java高精度 poj1001
- POJ1001 求高精度幂
- poj1001 + 高精度的心得
- poj1001 Exponentiation --高精度
- 求高精度幂 POJ1001
- POJ1001高精度小数运算
- poj1001-Exponentiation(高精度幂)
- POJ1001 求高精度幂
- POJ1001 Exponentiation【高精度】
- POJ1001 Exponentiation(高精度幂)
- JAVA学习笔记(二十一)- 集合 Set
- linux c 如何调用c++
- WP toolkit本地化
- linux学习之vim编辑器
- fatal error: call to undefined function curl_init() WAMP
- poj1001 高精度
- php开发之时间
- Oracle创建表空间、创建用户以及授权、查看权限
- Junit使用指南(一)
- 大话 Java File I/O(与c++对比)
- Junit使用指南(二)
- JAVA学习笔记(二十二)- 集合HashMap与Hashtable
- sublime text中添加diff跳转代码
- <犀牛书>注册文档加载完毕调用事件(可以早于load)