6-10 阶乘计算升级版 (20 分)
本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
void Print_Factorial ( const int N );
其中N
是用户传入的参数,其值不超过1000。如果N
是非负整数,则该函数必须在一行中打印出N
!的值,否则打印“Invalid input”。
裁判测试程序样例:
#include <stdio.h>
void Print_Factorial ( const int N );
int main()
{
int N;
scanf("%d", &N);
Print_Factorial(N);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
15
输出样例:
130767436800
void Print_Factorial ( const int N )
{
int a[5000],up=0,temp=0,A=N;
if(A<0)
{
printf("Invalid input");
return;
}
for(int k=1;k<5000;k++)
a[k]=0;
a[0]=1;
for(int i=1;i<=A;i++)
{
for(int j=0;j<5000;j++)
{
temp=(a[j]*i+up)/10;
a[j]=(a[j]*i+up)%10;
up=temp;
}
}
int t=4999;
while(a[t]==0&&t>0)
{
t--;
}
while(t+1)
{
printf("%d",a[t]);
t--;
}
}
原文链接: https://www.cnblogs.com/lxzbky/p/10470246.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/400196
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!