Factorials 阶乘
生活随笔
收集整理的這篇文章主要介紹了
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
7Sample 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
總結
以上是生活随笔為你收集整理的Factorials 阶乘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql 替换字段的一部分内容
- 下一篇: JS判断请求来自Android手机还是i