POJ 1001Exponentiation解题报告——求高精度幂
2017-12-31 23:14
435 查看
http://poj.org/problem?id=1001
这个道题比我想象中要难很多,做了很久,修改了很多次才搞定。
算法要点:
1. 使用字符串模拟数据相乘法.multiply函数就是将a*b的内容,放到字符串s中
2. 很重要:乘法都是从低位到高位乘,使用数组,需要把一个数(使用字符串表示)进行倒序,才方便计算。可以在最后都计算完以后,再把结果再倒序回来。
3. 很重要:要考虑边界的测试,需要准备一组测试数据。我在下面的代码里面增加了测试函数。如果#define TEST,那么会走测试流程,测试在data.txt中的数据。一定要保证所有的这些数据测试通过,才能在POJ上提交,否则往往会有问题。
data.txt我放在文章的最后。
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
这个道题比我想象中要难很多,做了很久,修改了很多次才搞定。
算法要点:
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) cca2 - 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
相关文章推荐
- POJ1001 求高精度幂 ACM解题报告(高精度模板)
- POJ 1001(浮点数高精度幂) 解题报告
- POJ 1001(浮点数高精度幂) 解题报告
- POJ 1503(高精度整数加法) 解题报告
- POJ 1001 解题报告 高精度大整数乘法模版
- POJ 1503(高精度整数加法) 解题报告
- POJ 1001 解题报告 高精度大整数乘法模版
- POJ 1013 Counterfeit Dollar 解题报告
- poj 2773 Happy 2006解题报告 <欧拉函数>
- poj 1068解题报告
- POJ-3349 Snowflake Snow Snowflakes 解题报告
- poj2377解题报告
- POJ 1753 Flip Game (状态压缩+BFS) -- 解题报告
- POJ-1321 棋盘问题 解题报告
- POJ_3278 Catch That Cow 解题报告
- POJ 2342&&HDU 1520解题报告
- poj1523 解题报告
- POJ-1905 Expanding Rods 解题报告
- POJ 3190 解题报告
- POJ 3468 A Simple Problem with Integers 解题报告