我想找出阶乘中尾随零的数量,但我的代码返回的是垃圾值。
#include<iostream>
using namespace std;
int main()
{
long int n;
cin>>n;
int z=0;
while(n>0)
{
n=n/5;
n=z+n;
z=n;
}
return z;
}
最佳答案
我理解你的代码如下:你的输入是 n
并且你想计算 n!
有多少个尾随零。在这种情况下,您的 while
循环必须如下所示:
while(n > 0)
{
n = n / 5;
z = z + n;
}
// After running the loop, directly output the result.
cout << "z: " << z;
这在 1000 的扩展中给了我 249 个尾随零! 4617 有 1151 个尾随零!
根据 this page那应该是正确的。
关于c++ - 为什么我的代码在阶乘返回垃圾值中查找尾随零的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775255/