java 编写一个计算零的递归函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13282913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:07:13  来源:igfitidea点击:

Writing A Recursive Function That Counts Zeros

javafunctionmethodsrecursion

提问by Matt Andrzejczuk

It is possible to count the number of zeros in an integer through a recursive method that takes a single int parameter and returns the number of zeros the parameter has.

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

So:

所以:

zeroCount(1000)

Would Return:

会回来:

3

You can remove the last digit from an integer by doing: "12345 / 10" = 1234

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

You can get the last digit from an integer by doing: "12345 % 10" = 5

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

This is what I have so far:

这是我到目前为止:

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

Does anyone have any suggestions or ideas for helping me solve this function?

有没有人有任何建议或想法可以帮助我解决此功能?

回答by Corbin

Run through your code in your head:

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

zeroCount(1000)

1000 % 10 == 0, so you're going to return 1000. That doesn't make sense.

1000 % 10 == 0,所以你要回来了1000。那没有意义。



Just pop off each digit and repeat:

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

It sounds like homework, so I'll leave the actual code to you, but it can be done as:

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

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

Note that without the terminating condition, it can recurse forever.

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

回答by Bhavik Shah

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); 
}

this would work

这行得通

回答by Rohit Jain

You have to invoke your recursive function from both if and else. Also, you were missing a Base Case: -

你必须从 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);
}

回答by Shurmajee

it is a simple problem and you don't need to go for recursion I think a better way would be converting the integer to a string and check for char '0'

这是一个简单的问题,您不需要进行递归我认为更好的方法是将整数转换为字符串并检查字符 '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;
}

回答by dreamcrash

You know that x % 10gives you the last digit of x, so you can use that to identify the zeros. Furthermore, after checking if a particular digit is zero you want to take that digit out, how? divide by 10.

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

public static int zeroCount(int num)
{
  int count = 0;

  if(num == 0) return 1;                  // stop case zeroCount(0)
  else if(Math.abs(num)  < 9)  return 0;  // stop case digit between 1..9 or -9..-1
  else
  {
   if (num % 10 == 0) // if the num last digit is zero
       count++; // count the zero, take num last digit out

   return count + zeroCount(num/10); // take num last digit out, and apply 
  } // the method recursively to the remaining digits 
}

I use math.Abs to allow negative numbers, you have to import java.lang.Math;

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

回答by Dee

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));
  }  
  }

回答by Sakshi Vij

There are three conditions here:
1. If number is single digit and 0 , then return 1
2. If number is less than 10 i.e. it is a number 1,2,3...9 then return 0
3. call recursion for zeros(number/10) + zeros(n%10)

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

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

n/10 will give us the n-1 digits from left and n%10 gets us the single digit. Hope this helps!

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

回答by Devendu Negi

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);
}

回答by Manish Kumar

 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 
   } 

回答by Shubham Chandgude

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);

}