1001. 求高精度幂

本文介绍了一种用于计算实数R的n次方(R^n)的算法,特别适用于高精度计算场景,例如金融计算等。算法能够处理数值较大且精度较高的计算任务,并确保输出结果的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1001. 求高精度幂

Description

对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。

现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。

Input

T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。

Output

对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。

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

代码

#include<iostream>
#include<string>
#include<vector>
#include <cmath>
using namespace std;
char* itoa(int num, char*str, int radix)
{/*索引表*/
    char index[] = "0123456789ABCDEF";
    unsigned unum;/*中间变量*/
    int i = 0, j, k;
    /*确定unum的值*/
    if (radix == 10 && num < 0)/*十进制负数*/
    {
        unum = (unsigned)-num;
        str[i++] = '-';
    }
    else unum = (unsigned)num;/*其他情况*/
                              /*转换*/
    do {
        str[i++] = index[unum % (unsigned)radix];
        unum /= radix;
    } while (unum);
    str[i] = '\0';
    /*逆序*/
    if (str[0] == '-')k = 1;/*十进制负数*/
    else k = 0;
    char temp;
    for (j = k; j <= (i - 1) / 2; j++)
    {
        temp = str[j];
        str[j] = str[i - 1 + k - j];
        str[i - 1 + k - j] = temp;
    }
    return str;
}
string getTimes(string s,string s2) {
    string res2;
    vector<string> res;
    for (int i = s2.size() - 1; i >= 0; i--) {
        int j = s.size() - 1, shang = 0;
        string ss;
        int down = s2[i] - '0';
        for (; j >= 0; j--) {
            int m = s[j] - '0';
            int temp = m*down + shang;
            if (j - 1 == -1) {
                char s3[25];
                ss.insert(0, itoa(temp, s3, 10));
            }
            else
                ss.insert(ss.begin(), temp % 10 + '0');
            shang = temp / 10;
        }
        if (res.size() > 0) {
            int m = res.size();
            while (m--) {
                ss += '0';
            }
        }
        //cout<<ss<<endl;           
        res.push_back(ss);
    }
    int end = res[res.size()-1].size() - 1;
    for (int m = 0; m < res.size(); m++) {
        int zero = end - res[m].size() + 1;
        while (zero--) {
            res[m].insert(res[m].begin(), '0');
        }
        //cout<<res[m]<<endl;
    }
    int shang = 0, sum = 0;
    end = res[0].size();
    while (end--) {
        sum = 0;
        for (int m = 0; m < res.size(); m++) {
            sum += (res[m][end] - '0');
        }
        sum += shang;
        //cout << sum << endl;
        if (end - 1 == -1) {
            char s3[25];
            res2.insert(0, itoa(sum, s3, 10));
        }
        else {
            res2.insert(res2.begin(), sum % 10 + '0');
        }
        shang = sum / 10;
    }
    return res2;
}

int main() {
    string s, res2;
    vector<string> res;
    double s4;
    int n;
    while (cin >> s >> n) {
        int count = 0;//小数点位数 
        //double resl=pow(s4, n);
        for (string::iterator iter = s.begin(); iter != s.end(); iter++, count++) {
            if (*iter == '.') {
                //cout<<"dsdsd"<<endl;
                count = (s.size() - count - 1)*n;
                //cout<<count;
                s.erase(iter);
                break;
            }
        }
        string s1 = s;
        if(n>1)
            s1 = getTimes(s, s);
        for (int i = 2; i < n; i++) {
            s1 = getTimes(s1, s);
        }
        int point_pos = s1.size() - count;
        s1.insert(point_pos, ".");
        if (s1[0] == '0')
            s1.erase(s1.begin());
        while (s1[s1.size()-1] == '0') {
            s1.erase(s1.end()-1);
        }
        if (s1[s1.size() - 1] == '.') {
            s1.erase(s1.end() - 1);
        }
        cout << s1 << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值