n! problem II | ||||||
| ||||||
Description | ||||||
求阶乘看起来是一个很简单的问题,现在我们要计算一个不超过100的整数的阶乘。 | ||||||
Input | ||||||
本题有多组测试数据,对于每组数据输入一个非负整数n(n不大于100),输入处理到文件结束。 | ||||||
Output | ||||||
输出n的阶乘并换行。 | ||||||
Sample Input | ||||||
1 2 3
| ||||||
Sample Output | ||||||
1 2 6
| ||||||
Source | ||||||
杨和禹求职记 | ||||||
Author |
首先认真审题,此题涉及到大数运算,具体代码如下:
#include<stdio.h>
#include<string.h>
int str[2000];
int main() { int n,p,j;
while(scanf("%d",&n)!=EOF){
memset(str,0,sizeof(str));
str[0]=1;
for(int i=2;i<=n;i++){
p=0; for(j=0;j<2000;j++){
int s=str[j]*i+p;
str[j]=s%10;
p=s/10; }
}
for(j=1999;j>=0;j--)
if(str[j]!=0) break;
for(int i=j;i>=0;i--) printf("%d",str[i]);
printf("\n");
}
return 0; }
原文:http://www.cnblogs.com/lxb1243816632/p/3539052.html