题目有多组数据,处理到文件结尾。输入一个数n。
输出阶乘,形式如:4! = 24.每组数据输出后跟一个空行。
1 4
0! = 1 1! = 1 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24
#include<stdio.h>
int main()
{
int i,j,n,h,s[1001];
while(~scanf("%d",&n))
{
printf("0! = 1\n");
for(i=0; i<1001; i++)
s[i]=0;
s[1]=1;
h=1;
for(i=1; i<=n; i++)
{
for(j=1; j<=h; j++)
s[j]*=i;
for(j=1; j<=h; j++)
if(s[j]>9)
{
s[j+1]+=s[j]/10;
s[j]=s[j]%10;
}
while(s[j]>9)
{
s[j+1]+=s[j]/10;
s[j]=s[j]%10;
j++;
}
for(;s[j]==0;j--);
h=j;
printf("%d! = ",i);
for(; j>0; j--)
printf("%d",s[j]);
printf("\n");
}
printf("\n");
}
return 0;
}