![](/img/trans.png)
[英]This code is running perfectly in codeblocks but giving a runtime error on ideone.com
[英]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;
}
我得到一些測試用例的正確答案。我在ideone中遇到的錯誤是運行時錯誤時間:0內存:3100信號:25
33628713 33628713 33628713 33628713 33628713 33628713
無輸入時輸出無限
我不知道對數在代碼中的作用。 您以某種方式使用它來確定何時停止除以5的冪。這似乎是一個近似值,並且如果有時您僅獲得正確的答案,那么我希望近似值的不准確性會對您造成不利影響。
您不需要對數。 當您確定不再有5的冪的因子時,可以簡單地停止循環。 考慮一下如何使用已經計算出的值來檢測到這一點。
您不需要任何近似值。 Purplemath上的頁面解釋並演示了如何計算階乘數中尾隨零的數量。 閱讀本文,看看您是否可以識別其中描述的技術與代碼的各個部分的對應關系,還請注意該頁面上沒有涉及對數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.