首页 > 代码库 > HDU 1042 N!【大数】
HDU 1042 N!【大数】
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 65350 Accepted Submission(s): 18696
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1 2 3
Sample Output
1 2 6
Author
JGShining(极光炫影)
思路:
i从1開始乘以a[1]并保存到a数组里面,假设a[1]里面的数大于10,对10取余。仅仅要它的个位数。将其与的取商进位。
在此之前一定要对c进行清零操作!
然后i再从a中的个位数開始。逐个与a 中的数相乘;(和小学我们列的竖式算乘法一样);就这样以此类推知道i等于n为止!
代码:
#include <stdio.h> #include <string.h> int a[110000]; int main() { int n,i,j,d,c; while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); d=1; a[1]=1; c=0; for(i=1;i<=n;i++)//须要乘的数。 { for(j=1;j<=d;j++)//j代表中间数值的位数!{ a[j]=a[j]*i+c;//相当于数学中的列竖式乘法的步骤! c=0;//每次进过位之后都得清零! if(a[j]>=10)//大于10的话须要保留个位,其它的数都进到前面!
{ c=a[j]/10; a[j]=a[j]%10; } } while(c!=0)//假设最高位也比10大。须要将其余数进到下一位。余数又一次赋0! { d++; a[d]=c%10; c=c/10; } } for(i=d;i>=1;i--)//终于从后往前逐个输出( printf("%d",a[i]);//这里面的d代表的就是算出来的数的最高位!
) printf("\n"); } return 0; }
HDU 1042 N!【大数】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。