-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_of_zeros.cpp
More file actions
68 lines (48 loc) · 1.17 KB
/
num_of_zeros.cpp
File metadata and controls
68 lines (48 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
链接:https://www.nowcoder.com/questionTerminal/6ffdd7e4197c403e88c6a8aa3e7a332a
来源:牛客网
[编程题]末尾0的个数
输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2
输入描述:
输入为一行,n(1 ≤ n ≤ 1000)
输出描述:
输出一个整数,即题目所求
示例1
输入
10
输出
2
*/
#include <iostream>
#include <math.h>
using namespace std;
int number_of_zeros(int n)
{
int number_of_two = 0;
int number_of_five = 0;
int j;
for (int i = 1; i <= n; i++) {
j = i;
while (!(j % 2))
{
number_of_two++;
j = j / 2;
}
j = i;
cout<<"i "<<i<<" number_of_two "<<number_of_two<<" number_of_five "<<number_of_five<<endl;
while (!(j % 5))
{
number_of_five++;
j = j / 5;
}
cout<<"i "<<i<<" number_of_two "<<number_of_two<<" number_of_five "<<number_of_five<<endl;
}
return number_of_two < number_of_five ? number_of_two : number_of_five;
}
int main()
{
int ret;
ret = number_of_zeros(10);
cout<<"result "<<ret<<endl;
return 0;
}