UVA - 748 Exponentiation

来源:互联网 发布:淘宝网开店费用 编辑:程序博客网 时间:2024/05/16 14:22

题目大意:求 R 的 n 次方

解题思路:循环高精度乘法

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctype.h>using namespace std;char R[10];int r[10];int ans[1000];int map[1000][1000];int main() {    int n;    while (scanf("%s%d", R, &n) != EOF) {        memset (ans, 0, sizeof(ans));        int p, len = strlen(R) - 1;        for (int i = len, j = 0; i >= 0; i--,j++) {            if (R[i] == '.') { p = j; j--; continue;}            r[j] = R[i] - '0';              ans[j] = r[j];        }        p *= n;        int tot = len;        n--;            while (n--) {            memset (map, 0, sizeof(map));            for (int i = 0; i < len; i++)                for (int j = 0; j < tot; j++) {                    map[i][i+j] += ans[j] * r[i];                    if (map[i][i+j] > 9) {                        map[i][i+j+1] += map[i][i+j]/10;                        map[i][i+j] %= 10;                    }                }            tot += len;            memset (ans, 0, sizeof(ans));            for (int i = 0; i < tot; i++)                for (int j = 0; j < len; j++) {                    ans[i] += map[j][i];                    if (ans[i] > 9) {                        ans[i+1]++;                        ans[i] -= 10;                    }                }        }        while (ans[tot] == 0) {            if (tot == p-1) break;            tot--;        }        while (tot != -1) {            int tag = 1, flag = 0;            if (ans[tot] == 0) {                flag = 1;                for (int i = tot; i >= 0; i--)                    if(ans[i] != 0) tag = 0;            }            if (flag && tag) break;            if (tot == p-1) printf(".");            printf("%d", ans[tot--]);        }        printf("\n");    }    return 0;}
0 0