poj 1001 Exponentiation 高精度乘法

本文介绍了一种通过大整数乘法实现精确计算实数R的n次幂的方法,适用于非常大的数值精度要求,例如计算国家债务等。文章提供了一个具体的C++实现案例,包括去除前导和后导零、字符串与整数数组之间的转换等功能。

Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 159384 Accepted: 38853

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 R n 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

Source


题意:

给出一个小数R,求R的n次幂是多少

思路:

可以转化为大整数的乘法运算

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
int n, a[MAXN], b[MAXN], c[MAXN];
char ans[MAXN];

//去前导零
void CutPrefix(char* _s) {
    int len = strlen(_s);
    int st = 0;
    while (st < len) {
        if (_s[st] != '0') {
            break;
        }
        ++st;
    }
    strncpy(_s, _s + st, len - st);
    _s[len - st] = 0;
}

//去后导零
void CutPostfix(char* _s) {
    int len = strlen(_s);
    for (int i = len - 1; i >= 0; --i) {
        if (_s[i] == '0') {
            _s[i] = 0;
        }
        else {
            break;
        }
    }
}

//char数转存到int数组
void StrToInt(int _i[], char _s[], int len) {
    memset(_i, 0, sizeof(_i));
    for (int i = 0; i < len; ++i) {
        _i[i] = _s[i] - '0';
    }
}

//int数组转存到char数组
void IntToStr(char _s[], int _i[], int len) {
    memset(_s, 0, sizeof(_s));
    for (int i = 0; i < len; ++i) {
        _s[i] = _i[i] + '0';
    }
}

//归整
void Check(int _a[], int len) {
    for (int i = 0; i < len; ++i) {
        if (_a[i] >= 10) {
            _a[i + 1] += _a[i] / 10;
            _a[i] %= 10;
        }
    }
}

//大数乘法
void Multiple(char* _a, char* _b) {
    int lena = strlen(_a);
    int lenb = strlen(_b);
    int len = lena + lenb;

    StrToInt(a, _a, lena);
    StrToInt(b, _b, lenb);
    reverse(a, a + lena);
    reverse(b, b + lenb);

    memset(c, 0, sizeof(c));
    for (int i = 0; i < lena; ++i) {
        for (int j = 0; j < lenb; ++j) {
            c[i + j] += a[i] * b[j];
        }
    }
    Check(c, len);
    reverse(c, c + len);
    IntToStr(_a, c, len);
    CutPrefix(_a);
}

int main() {
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
#endif
    char s[20];
    while (scanf("%s%d", s, &n) != EOF) {
        int loc = 0;
        int lens = strlen(s);
        while (loc < lens) {
            if (s[loc] != '.') {
                ++loc;
            }
            else {
                break;
            }
        }
        char s1[20] = "", s2[20] = "";
        strncpy(s1, s, loc);
        strncpy(s2, s + loc + 1, lens - loc);
        CutPrefix(s1);
        CutPostfix(s2);
        int lena = strlen(s1);
        int lenb = strlen(s2);
        strncpy(s1 + lena, s2, lenb);

        memset(ans, 0, sizeof(ans));
        ans[0] = '1';

        for (int i = 0; i < n; ++i) {
            Multiple(ans, s1);
        }

        int len = strlen(ans);
        int rcnt = lenb * n;

        if (lenb != 0) {
            CutPostfix(ans);
        }

        if (len <= rcnt) {
            printf(".");
            for (int i = len; i < rcnt; ++i) {
                printf("0");
            }
            printf("%s\n", ans);
        }
        else {
            int ed = len - rcnt;
            for (int i = 0; i < ed; ++i) {
                printf("%c", ans[i]);
            }
            if (rcnt != 0) printf(".%s\n", ans + ed);
            else printf("\n");
        }
    }
    return 0;
} 



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FksLiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值