poj 1001 大数模拟 +数据
求高精度幂
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 151545 | Accepted: 36902 |
Description
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
Input
T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output
对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
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
/* poj 1001 大数模拟 主要是有实数 所以要考虑小数点的个数还有前导0后导0, 明明R>0.0,n>0,但是后台数据竟然有n=0,R=0的情况 还有就是R不一定是实数 有可能是整数 */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define N 100000 #define LL long long using namespace std; int a[N], y[N]; int main() { char s[100]; int i, n; while(~scanf("%s %d%*c", s, &n)) { if(!n) { printf("1\n"); continue; } int b[100] = {0}; memset(a, 0, sizeof(a)); memset(y, 0, sizeof(y)); LL len = strlen(s), k = 6, sum = 1; for(i = 0; i < len; i++) if(s[i] == '.') break; int yy = i, j = 0;/*yy是看有没有小数点*/ k = len - i - 1;/*小数点的位置*/ if(i == 6) k = 0; else len = 5; for(i = 5; i >= 0; i--) if(s[i] != '.') b[j++] = a[j] = s[i] - '0'; sum = k * n;/*小数位数*/ int p = 5; if(yy == 6) p = 6; for(int u = 1; u < n; u++) { for(i = 0; i < p; i++) { for(j = 0; j < len; j++) { y[i + j] += a[j] * b[i]; y[i + j + 1] += y[i + j] / 10; y[i + j] %= 10; } } if(y[i + j] > 9) { y[i + j + 1] += y[i + j] / 10; y[i + j] %= 10; } len += p; for(j = 0; j < len; j++) a[j] = y[j]; memset(y, 0, sizeof(y)); } if(yy == 6) { for(i = 0; i < len; i++)/*是否全是0*/ if(a[i]) break; if(i == len) { printf("0\n"); continue; } for(i = len - 1; i >= 0; i--) if(a[i]) break; for(; i >= 0; i--) printf("%d", a[i]); printf("\n"); } else { for(i = 0; i < len; i++)/*是否全是0*/ if(a[i]) break; if(i == len) { printf("0\n"); continue; } for(i = 0; i < sum; i++)/*小数点后面是否全是0*/ if(a[i]) break; int pp = -1; if(i < sum) pp = i; for(i = len - 1; i >= sum; i--) if(a[i]) break; for(; i >= sum; i--) printf("%d", a[i]); if(pp != -1) { printf("."); for(; i >= pp; i--) printf("%d", a[i]); } printf("\n"); } } return 0; } /* 网上找的一些数据 只用了前一部分测 改完就A了 00.000 20 0 5.1004 15 41120989454.314570363993506408035342551967503175087477761156936917581824 6.0092 20 3769929003093657.321962960746396532078795815950689066104211891790874779993410876 17646994418302976 90.909 20 1486406551798253233999195346707087657544.775949857477432123445196642299879937148 848818861186416630801 1.0001 20 1.002001901140484655078767753259867978477279725977752387615504484511400190002000 01 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 .7329539704323545929059725326336036315520737374975804183549501881226320403884675 7925736111183672158735201714336872100830078125 0000.1 25 .0000000000000000000000001 0.0001 1 .0001 0.0000 20 0 11.001 20 673974233702250167359.9626948260256710473510810408353137341213656411623629902200 01 110000 20 67274999493256000920100000000000000000000000000000000000000000000000000000000000 000000000000000000000 .00001 25 .0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000001 .00010 23 .0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000001 1.0000 25 1 2.0000 25 33554432 6.0000 25 28430288029929701376 99.999 25 99975002999770012649468717709519310815545705768715.42652002479974457367312604296 4184298069822900531298735002299997000002499999 1 0 1 0 1 0 .00 1 0 95.123 12 548815620517731830194541.899025343415715973535967221869852721 0.4321 20 .0000000514855464107695612199451127676715483848176020072635120383542976301346240 1 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 */