改了下 能AC了
程序代码:
#include <stdio.h>
#include <string.h>
int fun(int a1[], int *n, int iLen) // Multiplication of large numbers
{
int i, j, len = iLen, len1 = iLen, k, l = 0, s;
int a[ 128 ] = { 0 };
int b[ 128 ] = { 0 };
int c[ 128 ] = { 0 };
int d[ 128 ] = { 0 };
int mul, m = *n;
for( i = 0; i < iLen; i++ )
b[ i ] = a[ i ] = a1[ i ];
while ( m - 1 )
{
for ( i = 0; i < len; i++ )
{
k = i;
for ( j = 0; j < len1; j++ )
{
mul = a1[ i ] * a[ j ] ;
if ( mul + c[ k ] + d[ k ] > 9 )
{
c[ k + 1 ] = ( mul + c[ k ] + d[ k ] ) / 10;
d[ k ] = ( mul + c[ k ] + d[ k ] ) % 10;
}
else
d[ k ] = mul + c[ k ] + d[ k ];
k++;
}
d[ k ] = c[ k ];
for ( j = 0; j <= k; j++ )
c[ j ] = 0;
}
len1 = k + 1;
for (j = 0; j <= len1; j++)
{
a[j] = d[j];
d[j] = 0;
}
m--;
}
i = len1;s = 0;
while( a[i] == 0 ) i--;
len1 = i + 1;
j = 0;
for (i = 0; i <= len1 ; i++)
a1[j++] = a[i];
j--;
return j ;
}
int main(void)
{
char a[ 128 ] = { 0 };
int b[ 128 ];
int n, i = 0, j = 0, cLen, iLen, strlen;
int first, tail, dot, dotsize;
int length, k;
while( scanf("%s %d", a, &n) != EOF )
{
for ( i = 127; i >= 0; i--)
b[ i ] = 0;
i = 0;
while( a[i] ) i++; // string length
cLen = i;
i = 0;
while( a[i] != '.' && a[ i ] ) i++; // dot position
{
if ( i == cLen ) dot = -1;
else dot = i;
}
i = 0;
while( a[i] == '0' || a[i] == '.' ) i++; // the first char that is
// not '0' and '.'
first = i;
i = dot + 1;
while( a[i] == '0' && a[i]) // the last number
i++;
if (i == cLen)
tail = dot - 1; // an integer tail
else
{
i = cLen - 1;
while( a[i] == '0' ) i--;
tail = i; // an decimal tail
}
j = 0;
for( i = tail; i >= first; i--) // convert the char array to int array
if( a[i] != '.')
{
b[j] = a[i] - '0';
j++;
}
iLen = j; // the length of int array
length = fun(b, &n, iLen); // call function
// return the length of int number without '.'
j = 0;
for ( i = length - 1; i >= 0; i--) // convert the int array to char array
{
a[ j ] = b[ i ] + '0';
j++;
}
length = j;
dotsize = ( tail - dot ) * n; // location of the dot
if (dotsize > 0)
{
if ( dot != -1 )
{
if ( length > dotsize )
{
strlen = length;
for ( i = length - 1; i >= length - dotsize; i--)
a[ i + 1 ] = a[ i ];
a[ i + 1 ] = '.';
}
else
{
strlen = k = dotsize;
for( i = length - 1; i >= 0; i--)
a[ k-- ] = a[ i ];
for ( i = k; i >= 1; i--)
a[ i ] = '0';
a[ 0 ] = '.';
}
}
else
{
strlen = length - 1;
j = 0;
for ( i = length - 1; i >= 0; i--)
{
a[ j ] = b[ i ] + '0';
j++;
}
}
}
else
{
strlen = length - 1;
j = 0;
for ( i = length - 1; i >= 0; i--)
{
a[ j ] = b[ i ] + '0';
j++;
}
}
for ( i = 0; i <= strlen; i++) // output the result
putchar( a[i] );
printf("\n");
}
return 0;
}
太冗长了……
[
本帖最后由 有容就大 于 2012-3-20 12:21 编辑 ]