/***作者:twopiece12**版本:0.9**/# include <conio.h># include <malloc.h># include <stdio.h># define MAX 100main(){ int *a; int i,k,count,j = 1; int n; int temp; a = (int *)malloc(sizeof(int)*MAX); printf("please input the number of n which you want to used to calculate the n! \n"); scanf("%d",&n); for (i=0; i<MAX; i++) { a[i] = 0; } a[MAX-1]=1; while (j <= n) { i = 1; while ( MAX-i >= 0) { a[MAX-i] *= j; i++; } i = 1; for (k=MAX-i; k>=0; k--) { while (a[k] > 9) { temp = a[k] / 10; a[k] = a[k] - (temp*10); a[k-1] += temp; } } j++; } printf("the answer is:"); i=0; for (i=0; i<MAX; i++) { if (a[i] != 0) { count = i; break; } } for (i=count; i<MAX; i++) { printf("%d",a[i]); } free(a); getch();} /*计算阶乘k! 采用已求得的阶乘(k-1)! 连续累加k-1次求出。以下是书本上的算法,并非原创。*/#include <stdio.h>#include <malloc.h>#define MAXN 1000void pnext(int a[],int k){ int *b, m = a[0],i,j,r,carry; b = (int *)malloc(sizeof(int)*(m + 1)); for (i=1; i<=m; i++) b[i] = a[i]; for (j=1; j<k; j++) { for (carry=0,i=1; i<=m; i++) { r = (i<=a[0]? a[i]+b[i] : a[i]) + carry; a[i] = r % 10; carry = r / 10; } if (carry) a[++m] = carry; } free(b); a[0] = m;}void write(int *a,int k){ int i; printf("%4d != ",k); for (i=a[0]; i>0; i--) printf("%d",a[i]); printf("\n\n"); }int main(){ int a[MAXN], n, k; printf("Enter the number n:"); scanf("%d",&n); a[0] = 1; a[1] = 1; write(a,1); for (k=2; k<=n; k++) { pnext(a,k); write(a,k); getchar(); } return (0); }

评论