Factorials 阶乘

Description

N的阶乘写作N!表示小于等于N的所有正整数的乘积。阶乘会很快的变大,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了。你的任务是找到阶乘最后面的非零位。举个例子,5!=1*2*3*4*5=120所以5!的最后面的非零位是2,7!=1*2*3*4*5*6*7=5040,所以最后面的非零位是4。

Input

共一行,一个整数不大于4,220的整数N。

Output

共一行,输出N!最后面的非零位。

Sample Input

7

Sample Output

4

题解:保留后几位,存在后导0时消去,最后对10求余即为答案。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 
 9 int main()
10 {
11     int n;
12     cin>>n;
13     long long ans = 1;
14     for(long long i = 1; i <= n; i ++)
15     {
16         ans *= i;
17         while(ans%10 == 0)
18             ans /= 10;
19         ans = ans%1000;
20     }
21     cout<<ans%10<<endl;
22     return 0;
23 }
View Code
原文地址:https://www.cnblogs.com/baiyi-destroyer/p/9560743.html