poj1001

来源:互联网 发布:中国茶叶出口数据2017 编辑:程序博客网 时间:2024/04/20 17:59

题目大意:

输入 R,n
where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Rn

解题思路:

由于题目给出的数R是有精度限制的,最长5位,所以最后的结果长度肯定小于25*6=150位,简单地应用一下高精度乘法即可。
注意末尾多余的0是不需要的。

代码:

// POJ : 1001// Notes : high precision multiplication & details processing#include<iostream>#include<string>using namespace std;int R[230]; // the length is up to 150 int ans[230],temp[230];int main(){    string s;    int n;    while (cin>>s>>n)    {        int point=0;        int l=0;        for (int i=0;i<230;i++)         {            R[i]=ans[i]=temp[i]=0;        }        for (int i=0;i<6;i++)        {            if (s[i]=='.') point=i;                          }        // right        int ls=5;        while (ls>point && s[ls]=='0') ls--; // abandon insignificant trailing zeros        for (int i=ls;i>point;i--)        {            l++;            R[l]=s[i]-'0';        }               int pointx = point;          point = (ls-point)*n;          // left        ls = 0;        while (s[ls]=='0' && ls<pointx) ls++; // abandon zeros at the beginning of integral part        for (int i=pointx-1;i>=ls;i--)        {            l++;            R[l]=s[i]-'0';        }        R[0] = l;                  ans[1] = 1;  // the transfer array        ans[0] = 1;        for (int t=1;t<=n;t++)        {            // temp = ans * R            for (int i=1;i<=ans[0];i++)            {                for (int j=1;j<=R[0];j++)                {                    temp[i+j-1]+=ans[i]*R[j];                                   }            }            // carry bit                        for (int i=1;i<=ans[0]+R[0]+1;i++)            {                temp[i+1]+=temp[i]/10;                temp[i]=temp[i]%10;                //cout<<temp[i];            }            // get length            for (int i=ans[0]+R[0]+1;i>=1;i--)            {                if (temp[i]!=0)                {                    temp[0]=i;                    break;                }            }            // last temp -> ans            ans[0]=temp[0];            for (int i=1;i<=temp[0];i++)            {                ans[i]=temp[i];                temp[i]=0;                            }        }        // print answer        if (point>ans[0]) ans[0]=point;        for (int i=ans[0];i>=1;i--)        {            if (i==point) cout<<'.';            cout<<ans[i];        }        cout<<endl;    }    return 0;}
0 0
原创粉丝点击