POJ-1001-Exponentiation-大数乘法

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 <iostream>

#include <string>
#include <cmath>
//#include <stdlib.h>
using namespace std;
//求一个数a的n次幂c
//主要是一些细节,前后0的处理,小数点的处理。
//将小数a转化为整数d,求整数的幂,记录a的小数位数(j-k),则c的小数位为(j-k)*n
char* 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;
}
posted @ 2012-09-11 23:56  疯狂青蛙  阅读(236)  评论(0编辑  收藏  举报