找规律:通过观察我们可以发现,末尾的0是由2*5构成的,那么我们只需要求每个因子分别贡献几个2几个5,由于5的比二大,故5的倍数一定比2的小,所以我们每次求出末位后还要进行乘2运算,乘2的个数为贡献5的个数和贡献2的个数的差
#include<iostream>
using namespace std;
int main(){
int n,cnt_2=0,cnt_5=0,res=1;
cin>>n;
for(int i=1;i<=n;i++){
int t=i;
while(t%2==0){//统计贡献2的个数
t/=2;
cnt_2++;
}
while(t%5==0){//统计贡献5的个数
t/=5;
cnt_5++;
}
res=res*t%10;//每次求末位
}
for(int i=0;i<cnt_2-cnt_5;i++)res=res*2%10;//补齐2和5的个数差
cout<<res<<endl;
return 0;
}