首页 > 代码库 > 51nod 1003 阶乘后面0的数量
51nod 1003 阶乘后面0的数量
题目意思:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1003
n的阶乘后面有多少个0?
6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0。
Input
一个数N(1 <= N <= 10^9)
Output
输出0的数量
Input 示例
5
Output 示例
1 题目分析: 对于本题,只需要知道在n!中,如果包含5的倍数,则末尾多一个0,如果包含5^2,末尾添加2个0,类推如果包含5^k, 末尾则加k个0。/** *阶乘后面的0的个数,如果n!的中包含一个5,就有一个0 *就有含有一个5就含有一个0,含有5^2就有2的0,依次同理 *例如15%5==0,就算包含5 */ #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int a[13];a[0]=5; for(int i=1;i<13;i++){ a[i]=a[i-1]*5; } //cout<<a[12]<<endl; int n; while(cin>>n){ int cnt=0; for(int k=0;k<13;k++){ for(int i=a[k];i<=n;i+=a[k]){ cnt++; } } cout<<cnt<<endl; } return 0; }
51nod 1003 阶乘后面0的数量
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。