poj 1001 Exponentiation 高精度乘法

    xiaoxiao2025-01-12  7

    Exponentiation Time Limit: 500MS Memory Limit: 10000KTotal 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

    East Central North America 1988

    题意:

    给出一个小数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; }

    转载请注明原文地址: https://ju.6miu.com/read-1295399.html
    最新回复(0)