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 }
?