HDU 1042(大数)
发布时间:2020-12-14 02:35:22 所属栏目:大数据 来源:网络整理
导读:? ? ? ? ? ? ? ? ? ? ? ?N! ? ?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. ? ?Sample Input ? ?1 ? ?2 ? ?3 ? ?Sample Output ? ?1 ? ?2 ? ?6 /
? ? ? ? ? ? ? ? ? ? ? ?N!
? ?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. ? ?Sample Input ? ?1 ? ?2 ? ?3 ? ?Sample Output ? ?1 ? ?2
? ?6
//由于大数求阶乘利用数组模拟乘法运算,所以必须从低位开始相乘 #include <stdio.h> #include <string.h> int a[50000]; int main() { int n; while(~scanf("%d",&n)) { memset(a,sizeof(a)); a[0]=1; int p=1; //p为阶乘所到位数的下一个所以输出时不需要判断0 for(int i=2;i<=n;i++) { int c=0,j; //c为相乘溢出的值 for(j=0;j<p;j++) { int s=a[j]*i+c; a[j]=s%10; c=s/10; } while(c) { a[j++]=c%10; c/=10; } p=j; } for(int j=p-1;j>=0;j--) printf("%d",a[j]); printf("n"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |