UVA - 748 Exponentiation

题目大意:求 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值