/*
阶乘最后一位非零数
*/
#include"bits/stdc++.h"
usingnamespacestd;
intexgcd(inta,intb,int&x,int&y,intc)
{
if(b==0)
{
x=c;
y=0;
returna;
}
intd=exgcd(b,a%b,x,y,c);
intt=x;
x=y;
y=t-a/b*y;
returnd;
}
intmain()
{
longlongfac[10]={1,1,2,6,24};
longlongN;
while(cin>>N)
{
intk=0;
longlongans=1;
while(N)
{
k+=N/5;
ans*=fac[N%5];
ans%=5;
N/=5;
}
k%=4;
ans*=1< ans%=5; intx,y; exgcd(5,2,x,y,-ans); intres=x*5+ans; res=(res%10+10)%10; cout< } } 扩展资料 阶乘亦递归方式定义:0!=1,n!=(n-1)!×n #include intmain() { intn,i; unsignedlonglongfactorial=1; printf("输入一个整数:"); scanf("%d",&n); //如果输入是负数,显示错误 if(n<0) printf("Error!负数没有阶乘jiechen"); else { for(i=1;i<=n;++i) { factorial*=i;//factorial=factorial*i; } printf("%d!=%llu",n,factorial); } return0; } 参考资料来源:百度百科—c语言
#include
void main()
{
int a=1,i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=a*i;
while(a%10==0)a=a/10;
a=a%10;
}
printf("%d\n",a);
}