原题:
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 R nwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
题意:
求超大数的n次方,poj1001,要求高精度。
题解:
绝望!大数乘法真的懵逼,wa了好久,怎么也算不出来.......
总而言之就是先找小数点的位置,然后模拟竖式乘法反复计算,最后再求出小数点的位置。因为输入时标准的六位数字,所以可以采用先不管小数点的位置的方法,因为最后小数点的位置就是开始的位置*n。然后再把前后的零去掉。
现在说着感觉贼简单,无限WA之绝望
#include<iostream>
using namespace std;
char str[10];
int n, dot;
int res[999999], a[999999], b[999999];
int len, lena, lenb;
void mul()
{
int i, j;
memset(res, 0, sizeof(res));
for (i=1; i<=lena; i++)
{
for (j=1; j<=lenb; j++)
{
res[i+j-1] += a[i] * b[j];
if (res[i+j-1]>9)
{
res[i+j] += res[i+j-1] / 10;
res[i+j-1] %= 10;
}
}
}
if (res[lena+lenb-1]>9)
{
res[lena+lenb] += res[lena+lenb-1] / 10;
res[lena+lenb-1] %= 10;
}
lena = lena + lenb;
for (i=1; i<=lena; i++) a[i] = res[i];
}
int main()
{
int i, j, up, down;
while (scanf("%s %d", str, &n)!=EOF)
{
dot = -1;
for (i=5, j=1; i>=0; i--)
{
if (str[i]!='.') a[j] = b[j++] = str[i] - '0';
else dot = i;
}
if (dot==-1) lena = lenb = 6;
else lena = lenb = 5;
for (i=1; i<n; i++) mul();
if (dot==-1)
{
for (i=lena; i>=1; i--) printf("%d", a[i]);
printf("\n");
}
else
{
dot = 5 - dot;
dot *= n;
for (i=1; i<=lena; i++)
{
if (a[i]!=0)
{
down = i;
break;
}
}
for (j=lena; j>=1; j--)
{
if (a[j]!=0)
{
up = j;
break;
}
}
i = up;
if (up<dot) i = dot;
j = down;
if (j>dot) j = dot + 1;
for (; i>=j; i--)
{
if (i==dot) printf(".");
printf("%d", a[i]);
}
printf("\n");
}
}
return 0;
}