简体   繁体   English

当我在ideone.com上运行时,在阶乘中尾随零。 甚至SPOJ也不接受

[英]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.

相关问题 这段代码可以在代码块中完美运行,但在ideone.com上给出了运行时错误 - This code is running perfectly in codeblocks but giving a runtime error on ideone.com 如何为ideone.com中的代码文件指定自定义文件名? - How can I give a custom file names to a code file in ideone.com? 为什么ideone.com这样做? - Why ideone.com does this? 为什么这个C ++代码在ideone.com上获得SIGILL? - Why does this C++ code get SIGILL on ideone.com? C++ 正则表达式在 ideone.com 上匹配,但在 Android NDK 构建中不匹配 - C++ regex matches on ideone.com but not in Android NDK build 程序在Visual Studio 2012中运行,但不在ideone.com中运行 - Program runs in Visual Studio 2012 but not ideone.com 无法弄清楚我的程序在spoj上给出运行时错误但在ideone上给出运行时错误的原因 - Can't figure out the reason why my program is giving runtime error on spoj but not on ideone Spoj中的SIGSEGV错误,但在ideone中工作正常 - SIGSEGV error in spoj but working fine in ideone 当我尝试运行代码时,在 vscode 中出现奇怪的错误 - Getting an weird error in vscode when i'm trying to run a code 我什至没有使用openframworks代码中的运行时错误 - Runtime error in openframworks code I'm not even using
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM