POJ 1001Exponentiation解题报告——求高精度幂

本文介绍了一种用于计算非常大数值幂次的方法,采用字符串模拟数据相乘的算法,并详细解释了如何处理边界条件及小数点的精确位置。此外,还提供了一个测试函数确保所有测试数据通过。

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

http://poj.org/problem?id=1001

这个道题比我想象中要难很多,做了很久,修改了很多次才搞定。
算法要点:
1. 使用字符串模拟数据相乘法.multiply函数就是将a*b的内容,放到字符串s中
2. 很重要:乘法都是从低位到高位乘,使用数组,需要把一个数(使用字符串表示)进行倒序,才方便计算。可以在最后都计算完以后,再把结果再倒序回来。
3. 很重要:要考虑边界的测试,需要准备一组测试数据。我在下面的代码里面增加了测试函数。如果#define TEST,那么会走测试流程,测试在data.txt中的数据。一定要保证所有的这些数据测试通过,才能在POJ上提交,否则往往会有问题。

data.txt我放在文章的最后。

/*
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 Rn 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 512
//#define TEST
char s[MAX],b[MAX],a[6];
int n,loc;

//1. 找到小数点位置,根据幂次计算小数点的位置
//2. 将s中小数点的位置替换为""
//3. 如果有小数点,将s中最后为0的部分干掉
void init(){
    int ln;
    int i;
    char *p = strstr(s,".");

    if(NULL!=p){
            ln = strlen(s) - (p-s) - 1;

            //remove the ending '0'
            for(i=strlen(s)-1;i>=ln;i--){
                if('0'==s[i])
                    ln--;
                else
                    break;
            }
            //remove '.'
            for(i=0;i<ln;i++){
                p[i]=p[i+1];
            }
            p[i]=0;
        }else{
            ln = 0;
        }

    //去掉开头的0
    for(i=0;i<strlen(s);i++){
        if('0'!=s[i]){
            break;
        }
    }
    if(i==strlen(s)){//all zero
        s[0]=0;
    }else{
        int j=0;
        for(i=i;i<=strlen(s);i++){
            s[j++]=s[i];
        }
    }

    loc = ln*n;
}
//s = a*b
void multiply(){
    int flag=0;
    for(int i=0;i<strlen(a);i++)
        for(int j=0;j<strlen(b);j++){
            s[i+j] += (a[i]-'0')*(b[j]-'0');
            s[i+j+1] += s[i+j]/10;
            s[i+j] %=10;
        }

    for(int i=(MAX-1);i>=0;i--){
        if(flag){
            s[i] +='0';
        }else{
            if(0!=s[i-1])
                flag=1;
        }
    }
}
void reverse(char *s){
    int size=strlen(s);

    char ch;
    for(int i=0;i<size/2;i++){
        ch=s[i];
        s[i]=s[size-i-1];
        s[size-i-1]=ch;
    }
}
void calculate(){
    reverse(s);
    strcpy(a,s);
    while(--n){
        strcpy(b,s);
        memset(s,0,MAX);
        multiply();
    }
    reverse(s);
}
void printToB(){
    int i;
    char *p=b;
    //需要考虑s的长度还不如小数点的长度
    if(loc>strlen(s)){
        int zero=loc-strlen(s);
        sprintf(p,".");
        p +=strlen(p);
        for(i=0;i<zero;i++){
            sprintf(p,"0");
            p +=strlen(p);
        }
        for(i=0;i<strlen(s);i++){
            sprintf(p,"%c",s[i]);
            p +=strlen(p);
        }
    }else{
        for(i=0;i<strlen(s);i++){
            if(loc>0 && (loc==(strlen(s)-i))) {
                sprintf(p,".");
                p +=strlen(p);
                }
            sprintf(p,"%c",s[i]);
            p +=strlen(p);
            }
    }
}

#if defined(TEST)
int main(){
    int i,j;
    char problem[512];
    char answer[512];
    char *file="data.txt";
    FILE *fh=fopen(file,"r");
    if(NULL==fh){
        printf("Cannot open %s\n",file);
        return 1;
    }
    while(!feof(fh)){
        fgets(problem,512,fh);
        fgets(answer,512,fh);
        for(i=0;i<strlen(problem);i++){
            if(' '!=problem[i])
                s[i]=problem[i];
            else{
                s[i]=0;
                break;
            }
        }
        for(i=i;i<strlen(problem);i++)
            if(' '!=problem[i])
                break;
        j=0;
        for(i=i;i<strlen(problem);i++)
            if(' '!=problem[i])
                a[j++]=problem[i];
            else{
                a[j++]=0;
                break;
            }
        n = atoi(a);

        init();
        //special case
        if(0==strlen(s)){
            printf("%d\n",0);
            continue;
        }else if(0==n){
            printf("%d\n",1);
            continue;
        }
        calculate();

        printToB();
        //check
        if('\n'==answer[strlen(answer)-1])
            answer[strlen(answer)-1]=0;
        if(strlen(b)!=strlen(answer)){
            printf("%s wrong!!!!\n",problem);
            printf("expected: %s\n",answer);
            printf("computed: %s\n",b);
            return 1;
        }else{
            for(i=0;i<strlen(b);i++){
                if(b[i]!=answer[i]){
                    printf("%s wrong!!!!\n",problem);
                    printf("expected: %s\n",answer);
                    printf("computed: %s\n",b);
                    printf("wrong location: %d\n",i);
                    return 1;
                }

            }
        }
    }
}
#else
int main(){
    while(EOF!=(scanf("%s %d",s,&n))){
        init();
        //special case
        if(0==strlen(s)){
            printf("%d\n",0);
            continue;
        }else if(0==n){
            printf("%d\n",1);
            continue;
        }

        calculate();
        printToB();
        printf("%s",b);
        printf("\n");
    }

    return 0;
}
#endif

data.txt:

00.000 20
0
5.1004 15
41120989454.314570363993506408035342551967503175087477761156936917581824
6.0092 20
3769929003093657.32196296074639653207879581595068906610421189179087477999341087617646994418302976
90.909 20
1486406551798253233999195346707087657544.775949857477432123445196642299879937148848818861186416630801
1.0001 20
1.00200190114048465507876775325986797847727972597775238761550448451140019000200001
54.120 20
46468190221655199272477781205783832.2745385034134800709627347812689616306176
000.10 20
.00000000000000000001
12.010 20
3898164373852177448724.9596914878392975482722144801842193624001
1.1000 20
6.72749994932560009201
00.100 20
.00000000000000000001
.10000 25
.0000000000000000000000001
.98765 25
.73295397043235459290597253263360363155207373749758041835495018812263204038846757925736111183672158735201714336872100830078125
0000.1 25
.0000000000000000000000001
0.0001 1
.0001
0.0000 20
0
11.001 20
673974233702250167359.962694826025671047351081040835313734121365641162362990220001
110000 20
67274999493256000920100000000000000000000000000000000000000000000000000000000000000000000000000000000
.00001 25
.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
.00010 23
.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
1.0000 25
1
2.0000 25
33554432
6.0000 25
28430288029929701376
99.999 25
99975002999770012649468717709519310815545705768715.426520024799744573673126042964184298069822900531298735002299997000002499999
1 0
1
0 1
0
.00 1
0
95.123 12
548815620517731830194541.899025343415715973535967221869852721
0.4321 20
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
5.1234 15
43992025569.928573701266488041146654993318703707511666295476720493953024
6.7592 9
29448126.764121021618164430206909037173276672
98.999 10
90429072743629540498.107596019456651774561044010001
1.0100 12
1.126825030131969720661201
.00001 1
.00001
.12345 1
.12345
0001.1 1
1.1
1.1000 1
1.1
10.000 1
10
000.10 1
.1
000000 1
0
000.00 1
0
.00000 0
1
000010 1
10
000.10 1
.1
0000.1 1
.1
00.111 1
.111
0.0001 1
.0001
0.0001 3
.000000000001
0.0010 1
.001
0.0010 3
.000000001
0.0100 1
.01
0.0100 3
.000001
0.1000 1
.1
0.1000 3
.001
1.0000 1
1
1.0000 3
1
1.0001 1
1.0001
1.0001 3
1.000300030001
1.0010 1
1.001
1.0010 3
1.003003001
1.0100 1
1.01
1.0100 3
1.030301
1.1000 1
1.1
1.1000 3
1.331
10.000 1
10
10.000 3
1000
10.001 1
10.001
10.001 3
1000.300030001
10.010 1
10.01
10.010 3
1003.003001
10.100 1
10.1
10.100 3
1030.301
99.000 1
99
99.000 3
970299
99.001 1
99.001
99.001 3
970328.403297001
99.010 1
99.01
99.010 3
970593.059701
99.100 1
99.1
99.100 3
973242.271
99.998 1
99.998
99.998 3
999940.001199992

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值