[英]trailing zeros in a factorial.when i run it on ideone.com i'm getting runtime error with infinite output. Even SPOJ is also not accepting
#include <iostream>
#include <cmath>
using namespace std;
int func(int n){
int count=0;
float t=log(n)*0.621;
int k=(int)t;
int temp;
for(int i=1;i<=k;i++){
int power=1;
for(int j=0;j<i;j++){
power=power*5;
}
temp=(int)(n/power);
count=count+temp;
}
return count;
}
int main(){
int t;
cin>>t;
for (int i=0;i<t;i++){
int n;
cin>>n;
cout<<func(n)<<'\n';
}
return 0;
}
I'm getting correct answer for few test cases.The error i'm getting in ideone is Runtime error time: 0 memory: 3100 signal:25 我得到一些测试用例的正确答案。我在ideone中遇到的错误是运行时错误时间:0内存:3100信号:25
33628713 33628713 33628713 33628713 33628713 33628713 33628713 33628713 33628713 33628713 33628713 33628713
with infinite output when no input is given 无输入时输出无限
I don't know what the logarithm is doing in your code. 我不知道对数在代码中的作用。 You somehow use it to determine when to stop dividing by powers of 5. It appears to be an approximation, and if you only get correct answers sometimes, then I expect that the approximation's inaccuracy is working against you. 您以某种方式使用它来确定何时停止除以5的幂。这似乎是一个近似值,并且如果有时您仅获得正确的答案,那么我希望近似值的不准确性会对您造成不利影响。
You don't need the logarithm. 您不需要对数。 You could simply stop the loop when you determine that there are no more factors of powers of 5 remaining. 当您确定不再有5的幂的因子时,可以简单地停止循环。 Think about how you could detect that using the values you've already calculated. 考虑一下如何使用已经计算出的值来检测到这一点。
You don't need to approximate anything. 您不需要任何近似值。 A page at Purplemath explains and demonstrates how to calculate the number of trailing zeroes in a factorial number. Purplemath上的页面解释并演示了如何计算阶乘数中尾随零的数量。 Read the article and see whether you can recognize how the technique described there corresponds to parts of your code, and also notice how there are no logarithms involved on that page. 阅读本文,看看您是否可以识别其中描述的技术与代码的各个部分的对应关系,还请注意该页面上没有涉及对数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.