Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找尾部0';s是一个数的阶乘 #包括 main(){ int n; scanf(“%d”和“&n”); int zz,计数; int i=5; 而(zz>=1){ zz=不适用; 计数+=zz; i=i*5; } printf(“%d”,计数); }_C_Windows_Ubuntu - Fatal编程技术网

查找尾部0';s是一个数的阶乘 #包括 main(){ int n; scanf(“%d”和“&n”); int zz,计数; int i=5; 而(zz>=1){ zz=不适用; 计数+=zz; i=i*5; } printf(“%d”,计数); }

查找尾部0';s是一个数的阶乘 #包括 main(){ int n; scanf(“%d”和“&n”); int zz,计数; int i=5; 而(zz>=1){ zz=不适用; 计数+=zz; i=i*5; } printf(“%d”,计数); },c,windows,ubuntu,C,Windows,Ubuntu,这是在数字的阶乘中查找尾部0的代码。 它在Ubuntu中的输出与在Windows上的不同。我不确定你到底在问什么,但我可以指出你的代码有几个问题: 缩进代码。缩进代码是必需的 试着在操作符周围加上空格。这将大大有助于提高代码的可读性 不要使用scanf。如果您避免使用整个scanf函数系列,您会更快乐 您正在使用zz和count而不进行初始化。C不将变量初始化为任何特定值;您始终必须自己初始化它们 您真的应该将main更改为intmain。他们做同样的事情,但后者更容易阅读 如果您添加一些测试用

这是在数字的阶乘中查找尾部0的代码。
它在Ubuntu中的输出与在Windows上的不同。

我不确定你到底在问什么,但我可以指出你的代码有几个问题:

  • 缩进代码。缩进代码是必需的
  • 试着在操作符周围加上空格。这将大大有助于提高代码的可读性
  • 不要使用
    scanf
    。如果您避免使用整个
    scanf
    函数系列,您会更快乐
  • 您正在使用
    zz
    count
    而不进行初始化。C不将变量初始化为任何特定值;您始终必须自己初始化它们
  • 您真的应该将
    main
    更改为
    intmain
    。他们做同样的事情,但后者更容易阅读

  • 如果您添加一些测试用例来解释代码应该做什么,那么回答您的问题就会更容易。

    您可以通过在编译过程中启用警告来发现至少一些问题。在这种情况下(从
    clang-Wall-Wextra the_file.c
    输出):

    您应该首先修复所有这些。

    未初始化变量

    tst.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    main() {
    ^
    tst.c:10:9: warning: variable 'count' is uninitialized when used here [-Wuninitialized]
            count += zz;
            ^~~~~
    tst.c:6:18: note: initialize the variable 'count' to silence this warning
        int zz, count;
                     ^
                      = 0
    tst.c:6:9: warning: variable 'zz' is used uninitialized whenever function 'main' is called [-Wsometimes-uninitialized]
        int zz, count;
        ~~~~^~
    tst.c:8:11: note: uninitialized use occurs here
        while(zz >= 1) {
              ^~
    tst.c:6:11: note: initialize the variable 'zz' to silence this warning
        int zz, count;
              ^
               = 0
    3 warnings generated.
    
    i=i*5溢出。未定义的行为。不同系统上的不同结果并不意外

    --

    不需要计算阶乘。只需计算
    n
    5
    s的数量即可

    int zz, count;
    int i = 5;
    while(zz >= 1) {  // what is zz?
    
    #包括
    内部主(空){
    int n;
    scanf(“%d”和“&n”);
    printf(“%d”,n/5);
    }
    15! --> 1307674368000
    9! --> 362880
    

    要有一个尾随0,事实需要一个5的倍数和2的倍数的乘积。阶乘组合中的每一个其他数字都是偶数,每5是5的倍数。

    main()
    isincorrect@Lưu Vĩnh Phúc缺少的返回类型没有问题,c将假定它是
    int
    ()
    的问题更大,但很少有编译器会遇到问题,而且这段代码的问题更严重。@Functino不在C99中。即使在隐式情况下,
    void
    仍然是必需的。正确的格式是
    intmain(void)
    intmain(intargc,char**argv)
    请您添加一些解释好吗?
    #include <stdio.h>
    int main(void) {
      int n;
      scanf("%d", &n);
      printf("%d", n/5);
    }
    
    15! --> 1307674368000
    9! --> 362880
    
    #include <stdio.h>
    int main(void) {
      int n;
      scanf("%d", &n);
      printf("%d", n/5);
    }
    
    15! --> 1307674368000
    9! --> 362880
    
         public static void main(String[] args) {
            int n=23;
            String fact= factorial(BigInteger.valueOf(23)).toString();
            System.out.format("Factorial value of %d is %s\n", n,fact);
            int len=fact.length();
            //Check end with zeros
            if(fact.matches(".*0*$")){
                String[] su=fact.split("0*$");
                //Split the pattern from whole string
                System.out.println(Arrays.toString(fact.split("0*$")));
               //Subtract from the total length 
                System.out.println("Count of trailing zeros "+(len-su[0].length()));
             }
    
        } 
    
         public static BigInteger factorial(BigInteger n) {
            if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
                return BigInteger.ONE;
            }
            return n.multiply(factorial(n.subtract(BigInteger.ONE)));
    
    
          }