本文主要是介绍poj1001 高精度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如题:http://poj.org/problem?id=1001
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 142484 | Accepted: 34813 |
Description
This problem requires that you write a program to compute the exact value of R n 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 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.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 高精度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!