为什么我写的 阶乘只能计算到33啊
程序代码:
int main() { int m,n,t; m=1; printf("输入一个数计算其阶乘:\n"); scanf("%d",&t); for(n=1;n<=t;n++) m=m*n; printf("阶乘:%d\n",m); }
到34就计算为0
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> short mul(short a[],short d,short x) { long i,y = 0; for(i = 0;i < d; i++) { y += (long)a[i] * (long)x; a[i] = (short)(y % 10000); y /= 10000; } a[d] = (short)y; return d+!!y; } int main() { time_t begin ,end; while(1) { time(&begin); long s; short *a,i,j,n,ws = 1; printf("N="); scanf("%d",&n); #define Pi 3.14159265358979323846L //这个语句什么意思? s=(long)((log(2*Pi*n)/2+n*(log(n)-1))/log(10)+1); a=(short*)malloc((s/4+2)*sizeof(short)); //申请空间 *a=1; for(i=2;i<=n;i++) ws=mul(a,ws,i); //函数调用了 printf("%d!=%d",n,a[ws-1]); for(j=ws-2;j>=0;j--) printf("%04d",a[j]); printf("\n"); free(a); time(&end); printf("\nRunning time is %lf\n",difftime(end,begin)); puts("continue(y/n)?"); getchar(); char sign = getchar(); if('y' == sign || 'Y' == sign) continue; else break; } system("pause"); return 0; }