Java Codingbat- Recursion1- count7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23136817/
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-08-13 21:00:10  来源:igfitidea点击:

Codingbat- Recursion1- count7

java

提问by Yossich

Can anybody help me programming the next problem (taken from Codingbat- Recursion1- count7)

谁能帮我编程下一个问题(取自Codingbat- Recursion1- count7

Given a non-negative int n,return the count of the occurrences of 7 as a digit, so for example 717 yields 2.(no loops). Note that mod (%)by 10 yields the rightmost digit (126 % 10 is 6),while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

给定一个非负int n,返回 的计数,occurrences of 7 as a digit例如717 yields 2.(无循环)。请注意,mod (%)除以 10 会产生rightmost digit (126 % 10 is 6),while 除以 (/) 以 10 删除最右边的数字(126 / 10 是 12)。

count7(717) → 2
count7(7) → 1
count7(123) → 0

There are some solutions which includes number of "returns". I would like to program the problem with only 1 "return".

有一些解决方案包括“回报”的数量。我想用只有 1 个“返回”来编程问题。

采纳答案by Samir Ghoneim

public int count7(int n) {
  int counter = 0;

  if( n % 10 == 7) counter++;

  if( n / 10  == 0)  return counter;

  return counter + count7(n/10); 
}

回答by Anirudh

Sure PFB my solution in JAVA for the same

当然PFB我在JAVA中的解决方案相同

public int count7(int n) {
   if((n / 10 == 0) && !(n % 10 == 7))      //First BASE CASE when the left most digit is 7 return 1
       return 0;     
   else if((n / 10 == 0) && (n % 10 == 7)) //Second BASE CASE when the left most digit is 7 return 0
        return 1;
   else if((n % 10 == 7))   
   //if the number having 2 digits then test the rightmost digit and trigger recursion trimming it there      
       return 1 + count7(n / 10);
   return count7(n / 10);
}

回答by Developer Marius ?il?nas

Well here's the solution I wroteand let's see how to do it with only one return

嗯,这是我写的解决方案,让我们看看如何只用一次返回

public int count7(int n) 
{
    int c = 0;
    if (7 > n)
    {
        return 0;
    }
    else
    {
        if ( 7 == n % 10)
        {
            c = 1;
        }
        else
        {
            c = 0;
        }
    }
    return c + count7(n / 10);  
}

the same with only one return

同样只有一次返回

public int count7(int n) 
{
    return (7 > n) ? 0 : ( ( 7 == n % 10) ? 1 + count7(n / 10) : 0 + count7(n / 10));  
}

回答by Bansari Desai

public int count7(int n) { 

  if(n == 0)
      return 0;
  else{
     if(n%10 ==7)
         return 1+count7(n/10);

     return 0+count7(n/10);  
  }
}

回答by Japhet Ndemera

public static int count7(int n){
        if(n == 7)
            return 1;
        else if(n > 9){
            int a = count7(n%10);
            int b = count7(n/10);
            return a + b;
        }else
            return 0;
}

回答by kurumkan

public int count7(int n) 
{  
 if(n==0)return 0;
 if(n%10==7)return 1+count7(n/10);
 else return count7(n/10);
}

回答by mark jerome lava

public int count7(int n) {
  if (n != 7 && n < 10) return 0;
  else if (n == 7) return 1;
  else if (n%10 == 7) return count7(n/10) + 1 ;
  else return count7(n/10);
}

回答by Ungeheuer

public int count7(int n){
    if(n < 7)
        return 0;
    else if(n % 10 == 7)
        return 1 + count7(n / 10);
    else
        return count7(n / 10);
}

the first if-statement is the base case that we would want to terminate on. The second checks to see if the rightmost digit is 7. If it is, chop the rightmost digit off and try again. When the recursive calls terminate and and values start getting returned up the chain, add 1 to include this successful check. In the case that neither of the above statements are true, chop off the rightmost digit and try again.

第一个 if 语句是我们想要终止的基本情况。第二个检查最右边的数字是否为 7。如果是,将最右边的数字砍掉,然后再试一次。当递归调用终止并且值开始返回链中时,添加 1 以包含此成功检查。如果以上陈述都不正确,请砍掉最右边的数字并重试。

I know this is 2 years old, but hope this is a bit more readable and intuitive, and thus helpful.

我知道这是 2 岁,但希望这更具可读性和直观性,从而有所帮助。

回答by Taladork

Using one return will likely make it harder to read. If you are counting occurrences in recursion, an easy formula is to create a base case to terminate on, then provide an incremental return, and finally a return that will aid in reaching the base case without incrementing. For example..

使用一次返回可能会使阅读变得更加困难。如果您计算递归中的出现次数,一个简单的公式是创建一个基本情况以终止,然后提供增量返回,最后提供一个有助于在不增加的情况下达到基本情况的返回。例如..

public int count7(int n) {
  if(n == 0) return 0;
  if(n % 10 == 7) return 1 + count7(n / 10);
  return count7(n / 10);
}

Using a one liner return like the one below in my opinion is harder to read or update because of the double ternary..

在我看来,使用像下面这样的单行返回更难阅读或更新,因为双三元..

public int count7(int n) 
{
    return (n == 0) ? 0 : (n % 10 == 7) ? 1 + count7(n / 10) : count7(n / 10);  
}

回答by Oscar Chambers

My solution works backwards from the nth digit to the first digit, by taking the modulus of the input. we add the number of sevens found into the return, for the final output.

我的解决方案通过取输入的模数从第 n 个数字向后工作到第一个数字。我们将找到的七位数添加到返回中,作为最终输出。

Then checking whether the input is less than 7 can be the next step. If the input is less than 7 then there have never been any 7s in the input to begin with.

然后检查输入是否小于7就可以进行下一步了。如果输入小于 7,则输入中从来没有任何 7 开始。

public int count7(int n) {
        int sevens_found = 0;
        if( n % 10 == 7 ) sevens_found ++;
            return ( n < 7) ? 0 : ( n % 10 == 7 ) ? sevens_found + count7 ( n / 10 ) : count7 ( n / 10 );
        }