我改进了一下,削减了while中的一个for语句
我使用TC3.0和Dev C++均能通过编译
#include <stdio.h>
#include <conio.h>
/*
-- Program detail -----------
Programmer : yuki
Last modify date: 2005-10-29
-----------------------------
*/
/*
NOTICE: If you want to placed this code to another bbs, please note the origin
*/
/* Macro definition as buffer size */
#define BUFFER_SIZE 2568
#define DELIMITER_WIDTH 4
/* Holding the result */
static int array[BUFFER_SIZE] = {0};
/* Holding the data length */
static int vaild_bit = 1;
int main() {
int N;
printf("Input N = ");
scanf("%d",&N);
/* Judge the data which inputed by user */
if(N < 0 || N > 1000) {
printf("Impossible!\n");
getch();
return 0;
}
else {
/* Set the base number as 1 */
array[0] = 1;
/* Integer varible i and j functioned as counter, temp using to calc */
register int i = 1,j,k,temp = -1;
/* General mutiplication */
while(i <= N) {
for(j = 0; j < vaild_bit; ++j) {
array[j] *= i;
if(temp >= 0) {
array[j] += temp/10;
temp = -1;
}
if(array[j] < 10 && j == vaild_bit-1) break;
else if(array[j] < 10) {
temp = -1;
continue;
}
temp = array[j];
array[j] = temp % 10;
/* Extend a bit for a new result */
if( temp+array[j+1]*(i+1) >= 10 && j == vaild_bit-1 ) ++vaild_bit;
}
/* Reset the length of new data */
vaild_bit = j + 1;
++i;
}
/* Result published */
printf("%d!(%d bit(s)) = \n",N,vaild_bit);
for( i = vaild_bit - 1; i >= 0; --i ) {
printf("%d",array[i]);
/* Every four figures delimited by a space */
if(!(i % DELIMITER_WIDTH)) putch(0x20);
}
printf("\n");
getch();
}
return 0;
}
下面附一个阶乘结果位数估计的方法:
对于n!,我们可以得其结果的长度便是log(1)+log(2)+log(3)+...+log(n),然后取整就是该阶乘结果位数的估计值。