【问题标题】:Writing A Recursive Function That Counts Zeros编写一个计数零的递归函数
【发布时间】:2012-10-28 06:54:24
【问题描述】:

可以通过递归方法计算整数中零的数量,该方法采用单个 int 参数并返回参数具有的零的数量。

所以:

zeroCount(1000)

将返回:

3

您可以通过执行以下操作从整数中删除最后一位数字:“12345 / 10” = 1234

您可以通过执行以下操作从整数中获取最后一位数字:“12345 % 10” = 5

这是我目前所拥有的:

public static int zeroCount(int num)
{
    if(num % 10 == 0)
        return num;
    else
        return zeroCount(num / 10);
}

有人有什么建议或想法可以帮我解决这个功能吗?

【问题讨论】:

  • 基本情况是错误的。有许多值 x % 10 为 0。(模运算应该很可能应该折叠到递归情况中。)

标签: java function methods recursion


【解决方案1】:

在你的脑海中运行你的代码:

zeroCount(1000)

1000 % 10 == 0,所以你要返回1000。这没有意义。


只需弹出每个数字并重复:

这听起来像是功课,所以我将把实际代码留给你,但可以这样做:

zeroes(0) = 1
zeroes(x) = ((x % 10 == 0) ? 1 : 0) + zeroes(x / 10)

注意,如果没有终止条件,它可以永远递归。

【讨论】:

    【解决方案2】:

    你必须从 if 和 else 中调用你的递归函数。此外,您还缺少一个基本案例:-

    public static int zeroCount(int num)
    {
        if(num % 10 == 0)
            return 1 + zeroCount(num / 10);
        else if (num / 10 == 0)
            return 0;
        else
            return zeroCount(num / 10);
    }
    

    【讨论】:

    • 这里没有基本情况。无限递归!
    • 这将导致StackOverflowException。缺少退出条件。 (OP 可以找到解决方案,只是让他知道;))
    • @Andreas_D.. 是的,我在最后一行引用了这一点。 :)
    • @BhavikShah.. 是吗?而且我不是来给 OP 提供确切的代码。他自己会弄清楚的。我刚刚指出他需要从 if 和 else 中调用他的方法。
    • 嘿伙计们,这不是 facebook
    【解决方案3】:
    public static int zeroCount(int num)
    {
        if(num == 0)
           return 0;
    
        if(num %10 ==0)
            return 1 + zeroCount(num / 10);
        else
            return zeroCount(num/10); 
    }
    

    这样就可以了

    【讨论】:

    • 非常感谢,现在我将尝试自己计算任何数字的个数。
    • 发生了一些无法解释的事情,我试图将这种递归方法分解成单独的部分,看看它是如何工作的。当方法开始并且 num = 1230005 时,if(num / 10 != 0) 为真,因此它向下移动到下一个 if 语句:if(num%10 == 0),并且 1230005 % 10 等于 0.5技术上为 0,因为它是 int 而不是 double。但是即使 num % 10 (1230005%10) 等于 0,它也不会返回 1 + zeroCount(num / 10),而是直接返回 else。我在这里想念什么?当该语句为真时,它如何跳过“if(num%10 == 0)”??
    【解决方案4】:

    这是一个简单的问题,你不需要递归 我认为更好的方法是将整数转换为字符串并检查 char '0'

    public static int zeroCount(int num)
    {
    String s=Integer.toString(num);
    int count=0;
    int i=0;
    for(i=0;i<s.length;i++)
    {
    if(s.charAt(i)=='0')
    {
    count++;
    }
    }
    return count;
    }
    

    【讨论】:

      【解决方案5】:

      您知道 x % 10 为您提供了 x 的最后一位数字,因此您可以使用它来识别零。此外,在检查特定数字是否为零之后,您想取出该数字,如何? 除以 10。

      public static int zeroCount(int num)
      {
        if(num == 0) return 1;      
        else if(Math.abs(num) < 9)  return 0;
        else return (num % 10 == 0) ? 1 + zeroCount(num/10) : zeroCount(num/10);
      }
      

      我使用math.Abs​​来允许负数,你必须导入java.lang.Math;

      【讨论】:

        【解决方案6】:
        import java.util.*;
        public class Count
        {
        static int count=0;
        static int zeroCount(int num)
        {
          if (num == 0){
             return 1;
          }
          else if(Math.abs(num) <= 9)
          { 
             return 0;
          } 
          else
          {
             if (num % 10 == 0)
             { // if the num last digit is zero
                count++;
               zeroCount(num/10);
             } // count the zero, take num last digit out
             else if (num%10 !=0){
                 zeroCount(num/10);
             }
          }
          return count;
          }
        
            public static void main(String[] args)
         {
          Scanner sc = new Scanner(System.in);
          System.out.print("Input: ");
          int num = sc.nextInt();
          System.out.println("Output: " +zeroCount(num));
          }  
          }
        

        【讨论】:

          【解决方案7】:

          这里有三个条件:
          1. 如果 number 是个位数且为 0 ,则返回 1
          2. 如果数字小于 10,即它是数字 1,2,3...9,则返回 0
          3.调用零递归(number/10) + zeros(n%10)

          zeros(number){
            if(number == 0 ) //return 1
            if(number < 10) //return 0
            else
                 zeros(number/10) + zeros(number%10)
          }
          

          n/10 将给我们从左边开始的 n-1 个数字,而 n%10 给我们一个数字。 希望这会有所帮助!

          【讨论】:

            【解决方案8】:
            public static int count_zeros(int n)
            {
                if(n<=9)
                {
                    if(n==0)
                    {  
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            
                int s=n%10;
            
                int count=0;
            
                if(s==0)
                {
                    count=1;
                }
            
                return count+count_zeros(n/10);
            }
            

            【讨论】:

              【解决方案9】:
               int countZeros(int n){
                   //We are taking care of base case 
              if(n<=9){         
                  if(n==0){
                       return 1;
                  }
               else
               {
                   return 0;
               } 
              }     
                 int last=n%10;  //last element of number for e.g- 20403, then last will give 3
                 int count=0;    //Initalsizing count as zero
                 if(last==0){    //We are checking either the last digit is zero or not if it will 
                      will update count from 0 to 1
                   count=1;
                }
                  return count+countZeros(n/10);  //Recursive call 
                 } 
              

              【讨论】:

              • 主要问题是基本情况,我们在这里缩进基本情况以将数字 0 计为一个 1 否。零,如果我们只保留 n==0 或 n==1 作为基本情况,那么在 n==0 的情况下,数字 0 将被视为 0 no。如果我们保持 n==1 也是错误的零,那么它会在返回计数时额外增加 1
              【解决方案10】:
              int check(int n){
                  if(n==0)
                      return 1;
                  return 0;
              
              }
              
              
              int fun(int n)
              {
                  if(n/10==0)
                  {
                      if(n==0){
                          return 1;
                      }
                      else{
                              return 0;
                  }
                  }
                  return check(n%10)+fun(n/10);
              
              }
              

              【讨论】:

                【解决方案11】:

                检查这个是否为正整数:

                 public static int zeroCount(int number) {
                    if (number == 0) {
                      return 1;
                    } else if (number <= 9) {
                      return 0;
                    } else {
                      return ((number % 10 == 0) ? 1 : 0) + zeroCount(number / 10);
                    }
                  }
                

                【讨论】:

                  【解决方案12】:

                  看看这个,这是我想出的解决方案。

                  int countZeros(int input){
                  //base case
                  if(input == 0){
                      return 1;
                  }
                  
                  int count = 0;
                  int lastDigit = input%10;
                  if(lastDigit == 0){
                    count = 1;
                  }
                  
                  //calc the smallInput for recursion
                  int smallInput = input/10;
                  //set smallAns = 0, if i/p itself is not 0 and no 0 is present then return smallAns = 0
                  int smallAns = 0;
                  //recursion call
                  if(smallInput != 0){
                      smallAns = countZerosRec(smallInput);            
                  }
                  
                  //if we get lastDigit = 0 then return smallAns + 1 or smallAns + count, else return smallAns  
                  if(lastDigit == 0){
                      return smallAns+count;
                  }
                  else{
                      return smallAns;
                  }}
                  

                  【讨论】:

                  • 您可能想解释一下您的解决方案。
                  【解决方案13】:
                  static int cnt=0;
                      public static int countZerosRec(int input) {
                          // Write your code here
                          if (input == 0) {
                              return 1;
                          }      
                          if (input % 10 == 0) {
                              cnt++;           
                          }
                          countZerosRec(input / 10);                  
                          return  cnt;
                      }
                  

                  【讨论】:

                    【解决方案14】:

                    使用递归的 CPP 代码:

                    int final=0;
                    int countZeros(int n)
                    {
                        if(n==0) //base case
                        return 1;
                        int firstn=n/10;
                        int last=n%10;
                        int smallop=countZeros(firstn);
                        if(last==0)
                            final=smallop+1;
                        return final;
                    }
                    

                    【讨论】:

                    • 嗨@UjjwalKarnanl - 只是想知道这对OP问题的2个热门答案(从2015年开始)真正增加了什么? [事实上,如果您查看它们,您可能会发现代码比它需要的更复杂!!]。
                    猜你喜欢
                    • 2015-03-04
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-03-19
                    • 2021-04-19
                    • 2013-12-06
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多