您的位置:首页 > 其它

POJ-1001-Exponentiation-大数乘法

2012-09-11 23:56 411 查看
DescriptionProblems 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.InputThe 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.OutputThe 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 Input95.123 120.4321 205.1234 156.7592 998.999 101.0100 12Sample Output548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

#include <iostream>

#include <string>#include <cmath>//#include <stdlib.h>using namespace std;//求一个数a的n次幂c//主要是一些细节,前后0的处理,小数点的处理。//将小数a转化为整数d,求整数的幂,记录a的小数位数(j-k),则c的小数位为(j-k)*nchar* multi(char *a, char *b) //两个整数的乘法{ int len1 = strlen(a); int len2 = strlen(b); int len = len1 + len2; char *c; c = (char*)malloc(sizeof(char)*len); memset(c, '0', sizeof(char)*len); int i, j, k = 0, temp = 0, p = 0, flag = 0, q = 0;; for(i = len1-1; i >= 0; i--) { for(j = len2-1; j>=0; j--) { temp = (a[i]-'0') * (b[j]-'0'); p = temp % 10; q = temp / 10; c[i+j+1] = c[i+j+1] + p; if((c[i+j+1]-'0') >= 10) { q++; c[i+j+1] = (c[i+j+1]-'0')%10 + '0'; } c[i+j] = c[i+j]+q; //进位位q k = 0; while((c[i+j-k]-'0') >= 10) //防止9999。。。的情况,一直进位,但进位永远不会超过1 { c[i+j-k] = (c[i+j-k]-'0')%10 + '0'; k++; c[i+j-k] = c[i+j-k] + 1; } } } c[len] = '\0'; //int z = 0; //while(c[z] == '0') //去掉前面的0 //{ // z++; //} //len -= z; //for(i = 0; i< len; i++) //{ // c[i] = c[i+z]; //} //c[len] = '\0'; return c;}char* iitoa(int val, char *buf, int ca = 10) //整数转发为字符串,itoa不是标准的,POJ不支持,可以用sprintf代替{ int i = 0; int temp = val; while(temp != 0) { temp /= 10; i++; } for(int j = i-1; j>=0; j--) { buf[j] = val%10 + '0'; val /= 10; } buf[i] = '\0'; return buf;}char* power(char *a, int n) //求一个整数a的n次幂{ char *c = NULL; c = (char*)malloc(200*sizeof(char)); memset(c, 0, 101); strcpy(c, a); for(int i = 0; i < n-1; i++) c = multi(c, a); return c;}int main(){ char a[10]; int n; char *c; while(cin >> a >> n) { if(n == 0) { cout << "1" << endl; continue; } int len1 = strlen(a); int i = 0, j = 0, k = 0, flag = 0; for(i = len1-1; i >= 0; i--) { if(a[i] == '.') { flag = 2; break; } if(a[i] == '0' && flag == 0) { k++; } if(a[i] != '0') flag = 1; j++; } double b = atof(a) + 0.00000000005; //转发为浮点数后有可能少1,比如1转为浮点数变为0.9999...-->在转化为整数时就为0了。 if(flag == 2) { for(i = 0; i < j-k; i++) { b *= 10; } } if(flag != 2) //没有小数点,即为整数的情况 { j = 0; k = 0; } char d[10] = {0}; iitoa((int)b, d, 10); //itoa北大居然不支持! c = (char*)malloc(200*sizeof(char)); memset(c, 0, 200); c = power(d, n); int len2 = strlen(c); int m = len2-(j-k)*n; if(m < 0) { for(i = len2; i >= 0; i--) { c[i-m] = c[i]; } for(i = 0; i < -m; i++) c[i] = '0'; } len2 = strlen(c); if(j-k == 0) { int z = 0; while(c[z] == '0') //去掉前面的0 { z++; } len2 -= z; for(i = 0; i< len2; i++) { c[i] = c[i+z]; } c[len2] = '\0'; cout << c << endl; continue; } for(i = len2; i > len2-(j-k)*n; i--) c[i] = c[i-1]; c[i] = '.'; len2++; c[len2] = '\0'; int z = 0; while(c[z] == '0') //去掉前面的0 { z++; } len2 -= z; for(i = 0; i< len2; i++) { c[i] = c[i+z]; } c[len2] = '\0'; cout << c << endl; } return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: