POJ 1001 Exponentiation

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 Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

解析

实数 R 的范围到 100(最多占据 5 位),指数 n 的范围到 25,因此会有至多 5 ∗ 25 = 125 5*25=125 525=125 位,所以用模拟来做。

可以注意到,给定 R 和 n 后,小数点的位置是确定的,因此转换成大整数乘法处理,最后添加小数点的位置。

此题需要注意特殊情况极多,我是参考评论区补充了很多特例才过的。但是 ac 并不代表代码没问题,边缘 case 还是很多的。

一些 case 参考

> .00001  1
> .12345  1
> 0001.1  1
> 1.1000  1
> 10.000  1
> 000.10  1
> 000000  1
> 000.00  1
> .00000  0
> 000010  1
> 000.10  1
> 0000.1  1
> 00.111  1
> 0.0001  1
> 0.0001  3
> 0.0010  1
> 0.0010  3
> 0.0100  1
> 0.0100  3
> 0.1000  1
> 0.1000  3
> 1.0000  1
> 1.0000  3
> 1.0001  1
> 1.0001  3
> 1.0010  1
> 1.0010  3
> 1.0100  1
> 1.0100  3
> 1.1000  1
> 1.1000  3
> 10.000  1
> 10.000  3
> 10.001  1
> 10.001  3
> 10.010  1
> 10.010  3
> 10.100  1
> 10.100  3
> 99.000  1
> 99.000  3
> 99.001  1
> 99.001  3
> 99.010  1
> 99.010  3
> 99.100  1
> 99.100  3
> 99.998  1
> 99.998  3

代码

#include <iostream>

#include <string>

#include <vector>

#include <cstdio>

using namespace std;

const int num = 200;

vector<int> multi(vector<int> A, int n)

{

    vector<int> C(num, 0);

    if(n == 1) return A;

    vector<int> B = A;

    int baseLen = A.size();

    int lenA = baseLen;

    int lenB = lenA;

    for (int i = 0; i < n - 1; i++)

    {

        int t;

        C.assign(num, 0);

        lenA = baseLen * (i + 1);

        for (int j = 0; j < lenB; j++)

        {

            for (int k = 0; k < lenA; k++)

            {

                t = C[j + k] + B[j] * A[k];

                C[j + k] = t % 10;

                C[j + k + 1] += t / 10;

            }

        }

  

        A = C;

    }

    return C;

}

int main()

{

    string s;

    int n;

    vector<int> base;

    while (cin >> s >> n)

    {

        base.clear();

        int decimalPoint;

        int sLength = s.size();

        int decimalFlag = 0;

        for (int i = sLength - 1; i >= 0; i--)

        {

            if (s[i] != '.'){

                base.push_back(s[i] - '0');

            } else {

                decimalFlag = 1;

                decimalPoint = sLength - i - 1;

            }

        }

        vector<int> res = multi(base, n);

        int resLength = base.size() * n;

        decimalPoint *= n;

        int zeroFlag = 1;

        if (n == 0)

        {

            cout << 1 << endl;

            continue;

        }

        else

        {

            for (int i = resLength - 1; i >= decimalPoint; i--)

            {

                if (zeroFlag && res[i] == 0)

                {

                    continue;

                }

                zeroFlag = 0;

                cout << res[i];

            }

            if(decimalFlag == 0){

                if(zeroFlag == 1) cout<<0;

                cout<<endl;

                continue;

            }

            int endPoint = 0;

            for (endPoint = 0; endPoint < decimalPoint; endPoint++)

            {

                if (res[endPoint] == 0)

                    continue;

                else

                    break;

            }

  

            if(zeroFlag == 1 && endPoint == decimalPoint){

                cout<<0<<endl;

                continue;

            }

  

            if( decimalPoint - 1 >= endPoint) {

                cout << '.';

            }

            for (int i = decimalPoint - 1; i >= endPoint; i--)

            {

                cout << res[i];

            }

            cout<<endl;

        }

    }

    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值