POJ——1001 Exponentiation 解题思路

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.
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 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

作为菜鸟,当然是此前从未见识过这种题目,单纯地做了n次乘方,显然是错误的,还有浮点数的不精确。
从百度上看到了大神的代码,用自己喜欢的方式写了一下贴上来,做一次搬运工。
http://zhidao.baidu.com/link?url=-GNoYiSci1_PVclcdZrFPhcFSWSAp1lYejVPxby5fx4hHxtrN33fWMA1V7MylQW3F6z0YdHmbojuDxf29dOSU_

解题思路:

  1. 获取小数点后小数的个数
  2. 将数字去掉无效的0和小数点放进数组
  3. 定义一个比较大的数组作为结果数组,放置每一次相乘的结果,数组每一个元素代表结果的每一位(根据数组顺序从高到低,从高位到低位)
  4. 结果数组的每一位与数相乘,放在当前的数组元素里面,结果数组都乘完后,将数组元素对10取取,其余的进到高位
  5. 最后得到结果数组,得到的结果小数个数=数的小数点*次数
  6. 结果输出
int Exponentiation(string s,int n)
    {
        int r[151];//结果数组
        int dotpoint=0;
        for(int t=0;t<150;t++)
        {
            r[t]=-1;//对结果数组初始化为-1,表示不存在有效数字
        }
        if (s.find('.')!=string::npos)
        {
            dotpoint=s.length()-s.find('.')-1;//获取小数点后数位的个数
        }
        string::iterator bend=s.end()-1;
        while(dotpoint>0&&bend>=s.begin())
        {
            if (*bend!='0')
            {
                break;
            }
            bend--;
            dotpoint--;//去掉无效的0
        }
        int cn=0;
        while(bend>=s.begin())
        {
            if (*bend!='.')
            {
                r[cn]=*bend-'0';//将字符转化为数字存入结果数组比如95.120,去掉0,再去掉小数点,那么r[0]=2,r[1]=1,r[2]=5,r[4]=9
                cn++;
            }
            if (bend==s.begin())
            {
                break;
            }
            bend--;
        }
        int m=0;
        for ( int k=cn-1;k>=0;k--)
        {
            m=m*10+r[k];//去掉无效0和小数点的数,比如输入95.120,那么m=9512
        }
        for(int i=1;i<n;i++)
        {
            int j=0;
            while(r[j]>-1)
            {
                r[j]=r[j]*m;//将结果数组的每一位与元素m相乘
                j++;
            }
            j=0;
            while(r[j]>-1)
            {
                if (r[j+1]==-1&&r[j]>=10)//如果高位为-1,则说明高位还没有有效数字,将取余后的数存于高位
                {
                    r[j+1]=r[j]/10;
                }
                else
                {
                    r[j+1]+=r[j]/10;//如果高位不为-1,说明高位有有效数,将取余后的数字与高位相加,存于高位
                }
                r[j]=r[j]%10;//对当前有效数字取余
                j++;
            }
        }

        dotpoint=dotpoint*n;//最终的小数个数
        int num=0;
        while(r[num]>-1)
        {
            num++;
        }
        if (dotpoint>num)//如果小数的个数大于结果数组的有效数的个数,则说明结果是小于1的小数,输出.和结果数组之前应有的0
        {

            cout<<".";
            while(dotpoint>num)
            {
                cout<<0;
                dotpoint--;
            }
            dotpoint=0;//将小数个数置为0,以判断小数点已经被输出。
        }
        for(int km=num-1;km>=0;km--)
        {
            if (km+1==dotpoint&&dotpoint!=0)//如果小数个数不为0并且当前输出的元素位置等于小数个数,则输出一个.
            {
                cout<<".";
            }
            cout<<r[km];
        }
        cout<<endl;
        return 1;

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值