#include <iostream>
using namespace std;
int countZeros(int n) {
int count = 0;
while (n >= 5) {
count += n / 5;
n /= 5;
}
return count;
}
int countOnes(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
int x = i;
while (x > 0) {
if (x % 10 == 1)
count++;
x /= 10;
}
}
return count;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
int zeros = countZeros(n);
int ones = countOnes(n);
cout << "Number of zeros in " << n << "! is: " << zeros << endl;
cout << "Number of ones in " << n << "! is: " << ones << endl;
return 0;
}
统计n!中数字“O“的个数,及尾部数字“0“的个数,
最新推荐文章于 2023-10-22 13:31:51 发布
这段代码展示了如何用C++编写函数来统计整数中零和一的个数,通过循环结构实现,适用于基础编程教学或理解算法应用。
1033

被折叠的 条评论
为什么被折叠?



